From b37a2a338202bbc85d524e8bc161f822b502a5aa Mon Sep 17 00:00:00 2001 From: Tomasz Palys Date: Fri, 13 Dec 2024 16:50:04 +0100 Subject: [PATCH] [native] Perform the backup restoration Summary: Restore the backup. When this operation finishes successfully, a user is navigated to the chat screen. | {F3482991} |{F3482992}| https://linear.app/comm/issue/ENG-9967/connect-the-siwe-logic-with-the-ui https://linear.app/comm/issue/ENG-9966/connect-the-password-logic-with-the-ui Depends on D14090 Test Plan: Go through the restore flow for both password and SIWE and check if the account was successfully restored. Reviewers: kamil, angelika, bartek Reviewed By: kamil Subscribers: ashoat Differential Revision: https://phab.comm.dev/D14165 --- native/account/restore-backup-screen.react.js | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/native/account/restore-backup-screen.react.js b/native/account/restore-backup-screen.react.js index 751181d653..c661f651d6 100644 --- a/native/account/restore-backup-screen.react.js +++ b/native/account/restore-backup-screen.react.js @@ -4,11 +4,21 @@ import * as React from 'react'; import { Text, View } from 'react-native'; import * as Progress from 'react-native-progress'; +import { getMessageForException } from 'lib/utils/errors.js'; + +import { setNativeCredentials } from './native-credentials.js'; import RegistrationContainer from './registration/registration-container.react.js'; import RegistrationContentContainer from './registration/registration-content-container.react.js'; +import { useRestore } from './restore.js'; import type { SignInNavigationProp } from './sign-in-navigator.react.js'; import type { NavigationRoute } from '../navigation/route-names.js'; import { useColors, useStyles } from '../themes/colors.js'; +import { + appOutOfDateAlertDetails, + unknownErrorAlertDetails, + userNotFoundAlertDetails, +} from '../utils/alert-messages.js'; +import Alert from '../utils/alert.js'; type Props = { +navigation: SignInNavigationProp<'RestoreBackupScreen'>, @@ -30,10 +40,60 @@ export type RestoreBackupScreenParams = { }, }; -// eslint-disable-next-line no-unused-vars function RestoreBackupScreen(props: Props): React.Node { const styles = useStyles(unboundStyles); const colors = useColors(); + + const { userIdentifier, credentials } = props.route.params; + + const restore = useRestore(); + React.useEffect(() => { + void (async () => { + try { + if (credentials.type === 'password') { + await restore(userIdentifier, credentials.password); + await setNativeCredentials({ + username: userIdentifier, + password: credentials.password, + }); + } else { + await restore( + userIdentifier, + credentials.secret, + credentials.message, + credentials.signature, + ); + } + } catch (e) { + const messageForException = getMessageForException(e); + console.log( + `Backup restore error: ${messageForException ?? 'unknown error'}`, + ); + let alertDetails = unknownErrorAlertDetails; + if ( + messageForException === 'user_not_found' || + messageForException === 'login_failed' + ) { + alertDetails = userNotFoundAlertDetails; + } else if ( + messageForException === 'unsupported_version' || + messageForException === 'client_version_unsupported' || + messageForException === 'use_new_flow' + ) { + alertDetails = appOutOfDateAlertDetails; + } + Alert.alert( + alertDetails.title, + alertDetails.message, + [{ text: 'OK', onPress: props.navigation.goBack }], + { cancelable: false }, + ); + } + })(); + // We want this effect to run exactly once + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + return (