summaryrefslogtreecommitdiffstats
path: root/src/features/auth/hooks/useRegister.ts
blob: 7b93e9220b09ccf3f93c9cc207fe2ab9fb9822e6 (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
40
41
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,
  };
}