diff options
| author | Alberto Duarte Delgado <alberto@albertoduarte.com> | 2026-03-30 14:59:30 +0100 |
|---|---|---|
| committer | Alberto Duarte Delgado <alberto@albertoduarte.com> | 2026-03-30 14:59:30 +0100 |
| commit | 6aede83b39f1050b7528b243278a85e5623856be (patch) | |
| tree | db0f2c89b18de3837f1bdf081409b09f9a763d5b /src/features | |
| parent | 83fd8f35b080e364e667697c12d4ace7f06450df (diff) | |
Integracion Firebase
Diffstat (limited to 'src/features')
| -rw-r--r-- | src/features/auth/hooks/useLogin.ts | 38 | ||||
| -rw-r--r-- | src/features/auth/hooks/useLogin.ts~ | 0 | ||||
| -rw-r--r-- | src/features/auth/hooks/useRegister.ts | 42 | ||||
| -rw-r--r-- | src/features/auth/hooks/useRegister.ts~ | 0 | ||||
| -rw-r--r-- | src/features/auth/services/authService.ts | 48 | ||||
| -rw-r--r-- | src/features/auth/services/authService.ts~ | 0 |
6 files changed, 128 insertions, 0 deletions
diff --git a/src/features/auth/hooks/useLogin.ts b/src/features/auth/hooks/useLogin.ts new file mode 100644 index 0000000..5e4c881 --- /dev/null +++ b/src/features/auth/hooks/useLogin.ts @@ -0,0 +1,38 @@ +import { useState } from 'react'; +import { router } from 'expo-router'; +import { authService } from '@/features/auth/services/authService'; + +export function useLogin() { + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const [isSubmitting, setIsSubmitting] = useState(false); + const [error, setError] = useState<string | null>(null); + + const submit = async () => { + try { + setError(null); + setIsSubmitting(true); + + await authService.login({ + email: email.trim(), + password, + }); + + router.replace('/(protected)/home'); + } catch (err: any) { + setError(err?.message ?? 'No se pudo iniciar sesión'); + } finally { + setIsSubmitting(false); + } + }; + + return { + email, + password, + isSubmitting, + error, + setEmail, + setPassword, + submit, + }; +} diff --git a/src/features/auth/hooks/useLogin.ts~ b/src/features/auth/hooks/useLogin.ts~ new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/features/auth/hooks/useLogin.ts~ diff --git a/src/features/auth/hooks/useRegister.ts b/src/features/auth/hooks/useRegister.ts new file mode 100644 index 0000000..7b93e92 --- /dev/null +++ b/src/features/auth/hooks/useRegister.ts @@ -0,0 +1,42 @@ +import { useState } from 'react'; +import { router } from 'expo-router'; +import { authService } from '@/features/auth/services/authService'; + +export function useRegister() { + const [displayName, setDisplayName] = useState(''); + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const [isSubmitting, setIsSubmitting] = useState(false); + const [error, setError] = useState<string | null>(null); + + const submit = async () => { + try { + setError(null); + setIsSubmitting(true); + + await authService.register({ + displayName: displayName.trim(), + email: email.trim(), + password, + }); + + router.replace('/(protected)/home'); + } catch (err: any) { + setError(err?.message ?? 'No se pudo registrar el usuario'); + } finally { + setIsSubmitting(false); + } + }; + + return { + displayName, + email, + password, + isSubmitting, + error, + setDisplayName, + setEmail, + setPassword, + submit, + }; +} diff --git a/src/features/auth/hooks/useRegister.ts~ b/src/features/auth/hooks/useRegister.ts~ new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/features/auth/hooks/useRegister.ts~ 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<User> { + 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<User> { + const userCredential = await signInWithEmailAndPassword(auth, email, password); + return userCredential.user; + }, + + async logout(): Promise<void> { + 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 --- /dev/null +++ b/src/features/auth/services/authService.ts~ |