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'; import { useTranslation } from '../../util/hooks/useTranslation'; 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); const { t } = useTranslation(); useEffect(() => { const getToken = async () => { const storedToken = await SecureStore.getItemAsync('accessToken'); setToken(storedToken); }; getToken(); }, []); const handleVerifyPassword = async (currentPassword: string) => { try { setIsLoading(true); if (!currentPassword) { setError(t('accountSettings.change_password.enter_password_error')); return; } if (!user || !user.email) { setError(t('accountSettings.change_password.user_verification_error')); return; } const success = await authenticationService.phoneLogin(user.phone, currentPassword); if (success) { setIsPasswordVerified(true); setError(''); return true; } else { setError(t('accountSettings.change_password.incorrect_password')); } } catch (error) { console.error('Error verifying password:', error); setError(t('accountSettings.change_password.incorrect_password')); return false; } finally { setIsLoading(false); } }; const handlePasswordChange = async () => { try { setIsLoading2(true); if (newPassword !== newConfirmPassword) { setError(t('accountSettings.change_password.password_mismatch')); return; } if (!newConfirmPassword || !token) { setError(t('accountSettings.change_password.enter_new_password')); return; } const success = await authenticationService.changePassword(newConfirmPassword, token); if (success) { setError(''); router.replace('accountMainPage'); } else { setError(t('accountSettings.change_password.change_password_failed')); } } catch (error) { console.error('Error changing password:', error); setError(t('accountSettings.change_password.general_error')); } finally { setIsLoading2(false); } }; return ( { if (router.canGoBack()) { router.back(); } else { router.replace('/accountMainPage'); } }} > {t('accountSettings.change_password.change_password_title')} {t('accountSettings.change_password.enter_current_password')} setCurrentPassword(t)} secureTextEntry={true} editable={!isPasswordVerified} /> {isLoading ? t('accountSettings.change_password.verifying') : isPasswordVerified ? t('accountSettings.change_password.verified') : t('common.next')} } 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 ? t('accountSettings.change_password.changing') : t('common.confirm')}} 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;