From 419115a072b59510063d03b7aa3f1902aa18e7b9 Mon Sep 17 00:00:00 2001 From: Thuc Nguyen Duy Date: Fri, 8 Sep 2023 16:08:41 +0700 Subject: [PATCH] [issue-444] Change password affect transactions flow --- .../ChangeMasterPassword/index.tsx | 76 +++++++++++-------- src/screens/MasterPassword/Login/index.tsx | 5 +- 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/screens/MasterPassword/ChangeMasterPassword/index.tsx b/src/screens/MasterPassword/ChangeMasterPassword/index.tsx index 4d7abb569..93d217f24 100644 --- a/src/screens/MasterPassword/ChangeMasterPassword/index.tsx +++ b/src/screens/MasterPassword/ChangeMasterPassword/index.tsx @@ -13,14 +13,12 @@ import { keyringChangeMasterPassword, keyringUnlock } from 'messaging/index'; import { useNavigation } from '@react-navigation/native'; import { RootNavigationProps } from 'routes/index'; import ChangeMasterPasswordStyle from './style'; -import { backToHome } from 'utils/navigation'; -import useGoHome from 'hooks/screen/useGoHome'; import i18n from 'utils/i18n/i18n'; import AlertBox from 'components/design-system-ui/alert-box'; import { FontSemiBold } from 'styles/sharedStyles'; import { useSelector } from 'react-redux'; import { RootState } from 'stores/index'; -import { createKeychainPassword, resetKeychainPassword } from 'utils/account'; +import { createKeychainPassword } from 'utils/account'; function checkValidateForm(isValidated: Record) { return isValidated.password && isValidated.repeatPassword; @@ -32,7 +30,6 @@ const ChangeMasterPassword = () => { const navigation = useNavigation(); const { isUseBiometric } = useSelector((state: RootState) => state.mobileSettings); const theme = useSubWalletTheme().swThemes; - const goHome = useGoHome(); const _style = ChangeMasterPasswordStyle(theme); const [isBusy, setIsBusy] = useState(false); const [errors, setErrors] = useState([]); @@ -63,15 +60,11 @@ const ChangeMasterPassword = () => { useHandlerHardwareBackPress(isBusy); const _backToHome = useCallback(() => { - backToHome(goHome); - }, [goHome]); - - async function handleUpdateKeychain(password: string) { - if (isUseBiometric) { - await resetKeychainPassword(); - createKeychainPassword(password); - } - } + navigation.reset({ + index: 0, + routes: [{ name: 'Home' }], + }); + }, [navigation]); const onSubmit = () => { if (checkValidateForm(formState.isValidated)) { @@ -80,28 +73,49 @@ const ChangeMasterPassword = () => { if (password && oldPassword) { setIsBusy(true); - keyringChangeMasterPassword({ - createNew: false, - newPassword: password, - oldPassword: oldPassword, - }) - .then(res => { - if (!res.status) { - setErrors(res.errors); - } else { - handleUpdateKeychain(password); - _backToHome(); + if (isUseBiometric) { + (async () => { + try { + const res = await createKeychainPassword(password); + if (!res) { + setIsBusy(false); + return; + } + handleUnlock(password, oldPassword); + } catch (e) { + setIsBusy(false); } - }) - .catch(e => { - setErrors([e.message]); - }) - .finally(() => { - setIsBusy(false); - }); + })(); + } else { + handleUnlock(password, oldPassword); + } } } }; + + function handleUnlock(password: string, oldPassword: string) { + keyringChangeMasterPassword({ + createNew: false, + newPassword: password, + oldPassword: oldPassword, + }) + .then(res => { + if (!res.status) { + setErrors(res.errors); + isUseBiometric && createKeychainPassword(password); + return; + } + _backToHome(); + }) + .catch(e => { + isUseBiometric && createKeychainPassword(password); + setErrors([e.message]); + }) + .finally(() => { + setIsBusy(false); + }); + } + const { formState, onChangeValue, onUpdateErrors, onSubmitField } = useFormControl(formConfig, { onSubmitForm: onSubmit, }); diff --git a/src/screens/MasterPassword/Login/index.tsx b/src/screens/MasterPassword/Login/index.tsx index a15fa6e8f..cea613e23 100644 --- a/src/screens/MasterPassword/Login/index.tsx +++ b/src/screens/MasterPassword/Login/index.tsx @@ -174,9 +174,10 @@ const Login: React.FC = ({ navigation }) => { async function requestUnlockWithBiometric() { try { const password = await getKeychainPassword(); - if (password) { - onUnlock(password); + if (!password) { + throw 'Biometry is not available'; } + onUnlock(password); } catch (e) { console.warn(e); if (JSON.stringify(e).indexOf('Biometry is not available') !== -1) {