summaryrefslogtreecommitdiffstats
path: root/src/features/auth/hooks/useLogin.ts
blob: 6af76c5e52345c96c32fbe683f24d416a1415aad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { useState } from 'react';
import { router } from 'expo-router';
import { authService } from '@/features/auth/services/authService';
import { getErrorMessage } from '@/shared/lib/errors/getErrorMessage';

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) {
      setError(getErrorMessage(err, 'No se pudo iniciar sesión'));
    } finally {
      setIsSubmitting(false);
    }
  };

  return {
    email,
    password,
    isSubmitting,
    error,
    setEmail,
    setPassword,
    submit,
  };
}