import { ThemedText } from '@/components/ThemedText'; import { ThemedView } from '@/components/ThemedView'; import { useAuth } from '@/contexts/auth-context'; import { fetchApi } from '@/lib/server-api-util'; import { User } from '@/types/user'; import { Ionicons } from '@expo/vector-icons'; import { useLocalSearchParams, useRouter } from 'expo-router'; import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { ActivityIndicator, KeyboardAvoidingView, Platform, ScrollView, TextInput, TouchableOpacity, View } from 'react-native'; const resetPassword = () => { const { t } = useTranslation(); const router = useRouter(); const { session_id: resetPasswordSessionId, token } = useLocalSearchParams<{ session_id: string; token: string }>(); // 使用 auth context 登录 const { login } = useAuth(); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [loading, setLoading] = useState(false); const [showPassword, setShowPassword] = useState(false); const [error, setError] = useState(''); const validatePassword = (pwd: string) => { const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/; return passwordRegex.test(pwd); }; const handleSubmit = async () => { if (!password) { setError(t('auth.login.passwordPlaceholder', { ns: 'login' })); return; } if (password !== confirmPassword) { setError(t('auth.signup.passwordNotMatch', { ns: 'login' })); return; } if (!validatePassword(password)) { setError(t('auth.signup.passwordAuth', { ns: 'login' })); return; } setLoading(true); setError(''); try { const body = { new_password: password, reset_password_session_id: resetPasswordSessionId, token }; const response = await fetchApi('/iam/reset-password', { method: 'POST', body: JSON.stringify(body), headers: { 'Content-Type': 'application/json' } }); if (login) { login(response, response.access_token || ''); } } catch (error) { console.error('Reset password error:', error); setError(t('auth.resetPwd.error', { ns: 'login' }) || 'Failed to reset password'); } finally { setLoading(false); } }; return ( {t('auth.resetPwd.title', { ns: 'login' })} {error ? ( {error} ) : null} setShowPassword(!showPassword)} className="p-2" > setShowPassword(!showPassword)} className="p-2" > {loading ? ( ) : ( {t('auth.resetPwd.resetButton', { ns: 'login' })} )} ); } export default resetPassword