import { View, Text, ScrollView, StyleSheet, Pressable } from 'react-native'; import { useContext, useEffect, useState } from 'react'; import { SafeAreaView } from 'react-native-safe-area-context'; import { router } from 'expo-router'; import { CrossLogoSvg } from '../global/SVG'; import { AuthContext } from '../../context/AuthProvider'; import NormalInput from '../global/normal_input'; import NormalButton from '../global/normal_button'; import * as SecureStore from 'expo-secure-store'; import { authenticationService } from '../../service/authService'; const changePasswordPageComponent = () => { const { user, setUser } = useContext(AuthContext); const [token, setToken] = useState(null); const [currentPassword, setCurrentPassword] = useState(null); const [newPassword, setNewPassword] = useState(null); const [newConfirmPassword, setNewConfirmPassword] = useState(null); const [isPasswordVerified, setIsPasswordVerified] = useState(false); const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const [isLoading2, setIsLoading2] = useState(false); useEffect(() => { const getToken = async () => { const storedToken = await SecureStore.getItemAsync('accessToken'); setToken(storedToken); }; getToken(); }, []); const handleVerifyPassword = async (currentPassword: string) => { try { setIsLoading(true); if (!currentPassword) { setError('請輸入密碼'); return; } if (!user || !user.email) { setError('無法驗證用戶,請重新登錄'); return; } console.log('user', user); const success = await authenticationService.phoneLogin(user.phone, currentPassword); if (success) { setIsPasswordVerified(true); setError(''); return true; } else { setError('密碼錯誤,請稍後再試'); } } catch (error) { console.error('Error verifying password:', error); setError('密碼錯誤,請稍後再試'); return false; } finally { setIsLoading(false); } }; const handlePasswordChange = async () => { try { setIsLoading2(true); if (newPassword !== newConfirmPassword) { setError('新密碼不相符'); return; } if (!newConfirmPassword || !token) { setError('請輸入新密碼'); return; } const success = await authenticationService.changePassword(newConfirmPassword, token); if (success) { setError(''); console.log('change password success'); router.replace('accountMainPage'); } else { setError('密碼更改失敗,請稍後再試'); } } catch (error) { console.error('Error changing password:', error); setError('發生錯誤,請稍後再試'); } finally { setIsLoading2(false); } }; return ( { if (router.canGoBack()) { router.back(); } else { router.replace('/accountMainPage'); } }} > 更改密碼 請輸入您現時的帳戶密碼 setCurrentPassword(t)} secureTextEntry={true} editable={!isPasswordVerified} /> {isLoading ? '驗證中...' : isPasswordVerified ? '已驗證' : '下一步'} } onPress={() => { if (currentPassword) { handleVerifyPassword(currentPassword); } }} disabled={isPasswordVerified} extendedStyle={isPasswordVerified == true ? { backgroundColor: '#70787C' } : {}} /> {isPasswordVerified && ( setNewPassword(t)} secureTextEntry={true} textContentType={'oneTimeCode'} /> setNewConfirmPassword(t)} secureTextEntry={true} textContentType={'oneTimeCode'} /> {isLoading2 ? '更改中...' : '確認'}} onPress={handlePasswordChange} /> )} {error && {error}} ); }; const styles = StyleSheet.create({ container: { flex: 1, marginHorizontal: 20 }, titleText: { fontSize: 24, fontWeight: '300' }, bottomContainer: { flex: 3, paddingBottom: 100 }, breakline: { width: 24, height: 1, backgroundColor: '#000000', marginVertical: 17 }, text: { fontSize: 18, paddingBottom: 10 }, hiddenPasswordFields: { gap: 10, paddingTop: 10 }, opacityZero: { opacity: 0 }, opacityFull: { opacity: 1 }, errorMessage: { fontSize: 14, color: '#ff0033', fontWeight: '400', marginLeft: 10, marginTop: 10 }, footer: { color: '#02677D', fontSize: 16, paddingVertical: 10 } }); export default changePasswordPageComponent; // 確認} // onPress={() => { // // poSSIBLY ADD ERROR HANDLING AND LOADING // // authenticationService.changePassword(password, token); // router.replace('accountMainPage'); // }} // />