From 6aede83b39f1050b7528b243278a85e5623856be Mon Sep 17 00:00:00 2001 From: Alberto Duarte Delgado Date: Mon, 30 Mar 2026 14:59:30 +0100 Subject: Integracion Firebase --- src/features/auth/services/authService.ts | 48 ++++++++++++++++++++++++++++++ src/features/auth/services/authService.ts~ | 0 2 files changed, 48 insertions(+) create mode 100644 src/features/auth/services/authService.ts create mode 100644 src/features/auth/services/authService.ts~ (limited to 'src/features/auth/services') diff --git a/src/features/auth/services/authService.ts b/src/features/auth/services/authService.ts new file mode 100644 index 0000000..54ff58f --- /dev/null +++ b/src/features/auth/services/authService.ts @@ -0,0 +1,48 @@ +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); + }, +}; diff --git a/src/features/auth/services/authService.ts~ b/src/features/auth/services/authService.ts~ new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3-54-g00ecf