import { Ionicons } from "@expo/vector-icons"; import { router } from "expo-router"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { ActivityIndicator, TextInput, TouchableOpacity, View } from "react-native"; import { useAuth } from "../../contexts/auth-context"; import { fetchApi } from "../../lib/server-api-util"; import { User } from "../../types/user"; import { ThemedText } from "../ThemedText"; const REMEMBER_ACCOUNT_KEY = 'fairclip_remembered_account'; interface LoginProps { updateUrlParam: (status: string, value: string) => void; setError: (error: string) => void; setShowPassword: (showPassword: boolean) => void; showPassword: boolean; } const Login = ({ updateUrlParam, setError, setShowPassword, showPassword }: LoginProps) => { const { t } = useTranslation(); const { login } = useAuth(); const [isLoading, setIsLoading] = useState(false); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [rememberMe, setRememberMe] = useState(false); const handleLogin = async () => { if (!email) { setError(t('auth.login.emailPlaceholder', { ns: 'login' })); return; }; if (!password) { setError(t('auth.login.passwordPlaceholder', { ns: 'login' })); return; } setIsLoading(true); try { const body = { account: email, password: password, }; const res = await fetchApi('/iam/login/password-login', { method: 'POST', body: JSON.stringify(body), }); login({ ...res, email: res?.account }, res.access_token || ''); const userInfo = await fetchApi("/iam/user-info"); if (userInfo?.nickname) { router.replace('/ask'); } else { router.replace('/user-message'); } } catch (error) { } finally { setIsLoading(false); } }; const handleForgotPassword = () => { updateUrlParam('status', 'forgetPwd'); }; const handleSignUp = () => { updateUrlParam('status', 'signUp'); }; return {/* 邮箱输入框 */} {t('auth.login.email', { ns: 'login' })} { setEmail(text); setError('123'); }} keyboardType="email-address" autoCapitalize="none" /> {/* 密码输入框 */} {t('auth.login.password', { ns: 'login' })} { setPassword(text); setError('123'); }} secureTextEntry={!showPassword} /> setShowPassword(!showPassword)} > {/* 忘记密码链接 */} {t('auth.login.forgotPassword', { ns: 'login' })} {/* 登录按钮 */} {isLoading ? ( ) : ( {t('auth.login.loginButton', { ns: 'login' })} )} {/* 注册链接 */} {t('auth.login.signUpMessage', { ns: 'login' })} {t('auth.login.signUp', { ns: 'login' })} } export default Login