import { createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut, updateProfile, User, } from 'firebase/auth'; import { doc, serverTimestamp, setDoc } from 'firebase/firestore'; import { auth, db } from '@/shared/lib/firebase/config'; type RegisterParams = { email: string; password: string; displayName: string; }; type LoginParams = { email: string; password: string; }; export const authService = { async register({ email, password, displayName }: RegisterParams): Promise { const userCredential = await createUserWithEmailAndPassword(auth, email, password); const user = userCredential.user; if (displayName.trim()) { await updateProfile(user, { displayName }); } await setDoc(doc(db, 'users', user.uid), { email: user.email, displayName: displayName.trim(), createdAt: serverTimestamp(), }); return user; }, async login({ email, password }: LoginParams): Promise { const userCredential = await signInWithEmailAndPassword(auth, email, password); return userCredential.user; }, async logout(): Promise { await signOut(auth); }, };