All files / app/login/hooks useLoginForm.ts

66.66% Statements 22/33
50% Branches 4/8
83.33% Functions 5/6
64.51% Lines 20/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                          17x 17x 17x 17x   17x   17x   2x   2x   2x     2x   2x                                           17x 3x 3x     17x 3x 3x     17x 1x     17x                      
import { useState } from 'react';
import { useRouter } from 'expo-router';
import { ZodError } from 'zod';
 
import { useUserStore } from '@/src/store/userStore';
import { loginSchema } from '@/src/schemas/auth.schema';
import { toast } from '@/src/components/dialogs';
 
/**
 * 登录表单 Hook
 * 负责登录表单的状态管理和验证
 */
export function useLoginForm() {
  const router = useRouter();
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [errors, setErrors] = useState<{ email?: string; password?: string }>({});
 
  const { login, isLoading } = useUserStore();
 
  const handleLogin = async () => {
    // 清除之前的错误
    setErrors({});
 
    try {
      // 使用 Zod 验证表单数据
      loginSchema.parse({ email, password });
 
      // 验证通过,执行登录
      await login(email, password);
      // 登录后始终显示欢迎界面
      router.replace('/onboarding');
    } catch (error) {
      if (error instanceof ZodError) {
        // 处理验证错误
        const fieldErrors: { email?: string; password?: string } = {};
        error.errors.forEach((err) => {
          const field = err.path[0] as 'email' | 'password';
          fieldErrors[field] = err.message;
        });
        setErrors(fieldErrors);
 
        // 显示第一个错误
        const firstError = error.errors[0];
        toast.error('验证失败', firstError.message);
      } else if (error instanceof Error) {
        // 处理 API 错误
        toast.error('登录失败', error.message);
        console.error('登录错误:', error);
      }
    }
  };
 
  const handleEmailChange = (text: string) => {
    setEmail(text);
    if (errors.email) setErrors({ ...errors, email: undefined });
  };
 
  const handlePasswordChange = (text: string) => {
    setPassword(text);
    if (errors.password) setErrors({ ...errors, password: undefined });
  };
 
  const navigateToRegister = () => {
    router.push('/register' as any);
  };
 
  return {
    email,
    password,
    errors,
    isLoading,
    handleEmailChange,
    handlePasswordChange,
    handleLogin,
    navigateToRegister,
  };
}