diff --git a/native/backup/backup-handler.js b/native/backup/backup-handler.js --- a/native/backup/backup-handler.js +++ b/native/backup/backup-handler.js @@ -29,6 +29,7 @@ const latestBackupInfo = useSelector( state => state.backupStore.latestBackupInfo, ); + const userIdentifier = useSelector(state => state.currentUserInfo?.username); const dispatchActionPromise = useDispatchActionPromise(); const { createUserKeysBackup, retrieveLatestBackupInfo, getBackupUserKeys } = useClientBackup(); @@ -81,10 +82,10 @@ if (!latestBackupInfo?.backupID) { return; } - const { - latestBackupInfo: { backupID }, - userIdentifier, - } = await retrieveLatestBackupInfo(); + if (!userIdentifier) { + throw new Error('Missing userIdentifier'); + } + const { backupID } = await retrieveLatestBackupInfo(userIdentifier); const backupSecret = await getBackupSecret(); const [ @@ -125,6 +126,7 @@ getBackupUserKeys, latestBackupInfo?.backupID, retrieveLatestBackupInfo, + userIdentifier, ]); React.useEffect(() => { diff --git a/native/backup/use-client-backup.js b/native/backup/use-client-backup.js --- a/native/backup/use-client-backup.js +++ b/native/backup/use-client-backup.js @@ -20,10 +20,9 @@ type ClientBackup = { +createFullBackup: () => Promise, +createUserKeysBackup: () => Promise, - +retrieveLatestBackupInfo: () => Promise<{ - +latestBackupInfo: LatestBackupInfo, - +userIdentifier: string, - }>, + +retrieveLatestBackupInfo: ( + userIdentifier: string, + ) => Promise, +getBackupUserKeys: ( userIdentifier: string, backupSecret: string, @@ -47,11 +46,22 @@ ); } +async function retrieveLatestBackupInfo( + userIdentifier: string, +): Promise { + const response = + await commCoreModule.retrieveLatestBackupInfo(userIdentifier); + + return assertWithValidator( + JSON.parse(response), + latestBackupInfoResponseValidator, + ); +} + function useClientBackup(): ClientBackup { const currentUserID = useSelector( state => state.currentUserInfo && state.currentUserInfo.id, ); - const currentUserInfo = useSelector(state => state.currentUserInfo); const loggedIn = useSelector(isLoggedIn); const getBackupSecret = useGetBackupSecretForLoggedInUser(); @@ -92,22 +102,6 @@ ); }, [loggedIn, currentUserID, getBackupSecret, authVerifiedEndpoint]); - const retrieveLatestBackupInfo = React.useCallback(async () => { - if (!loggedIn || !currentUserID || !currentUserInfo?.username) { - throw new Error('Attempt to restore backup for not logged in user.'); - } - const userIdentifier = currentUserInfo?.username; - - const response = - await commCoreModule.retrieveLatestBackupInfo(userIdentifier); - - const latestBackupInfo = assertWithValidator( - JSON.parse(response), - latestBackupInfoResponseValidator, - ); - return { latestBackupInfo, userIdentifier }; - }, [currentUserID, currentUserInfo, loggedIn]); - return React.useMemo( () => ({ createFullBackup, @@ -115,7 +109,7 @@ retrieveLatestBackupInfo, getBackupUserKeys, }), - [createFullBackup, createUserKeysBackup, retrieveLatestBackupInfo], + [createFullBackup, createUserKeysBackup], ); } diff --git a/native/profile/backup-menu.react.js b/native/profile/backup-menu.react.js --- a/native/profile/backup-menu.react.js +++ b/native/profile/backup-menu.react.js @@ -47,6 +47,9 @@ invariant(identityContext, 'Identity context should be set'); const { identityClient, getAuthMetadata } = identityContext; + const userIdentifier = useSelector(state => state.currentUserInfo?.username); + invariant(userIdentifier, 'userIdentifier should be set'); + const { createFullBackup, retrieveLatestBackupInfo, @@ -81,15 +84,17 @@ const testRestoreForPasswordUser = React.useCallback(async () => { let message = 'success'; try { - const [{ latestBackupInfo, userIdentifier }, backupSecret] = - await Promise.all([retrieveLatestBackupInfo(), getBackupSecret()]); + const [{ backupID }, backupSecret] = await Promise.all([ + retrieveLatestBackupInfo(userIdentifier), + getBackupSecret(), + ]); const { backupDataKey, backupLogDataKey } = await getBackupUserKeys( userIdentifier, backupSecret, - latestBackupInfo.backupID, + backupID, ); await commCoreModule.restoreBackupData( - latestBackupInfo.backupID, + backupID, backupDataKey, backupLogDataKey, persistConfig.version.toString(), @@ -100,13 +105,18 @@ console.error(message); } Alert.alert('Restore protocol result', message); - }, [getBackupSecret, getBackupUserKeys, retrieveLatestBackupInfo]); + }, [ + getBackupSecret, + getBackupUserKeys, + retrieveLatestBackupInfo, + userIdentifier, + ]); const testLatestBackupInfo = React.useCallback(async () => { let message; try { - const { latestBackupInfo } = await retrieveLatestBackupInfo(); - const { backupID, userID } = latestBackupInfo; + const { backupID, userID } = + await retrieveLatestBackupInfo(userIdentifier); message = `Success!\n` + `Backup ID: ${backupID},\n` + @@ -119,7 +129,7 @@ console.error(message); } Alert.alert('Latest backup info result', message); - }, [currentUserInfo?.id, retrieveLatestBackupInfo]); + }, [currentUserInfo?.id, userIdentifier, retrieveLatestBackupInfo]); const testSigning = React.useCallback(async () => { // This test only works in the following case: @@ -130,10 +140,8 @@ // 5. Perform this test let message; try { - const { - latestBackupInfo: { userID, backupID }, - userIdentifier, - } = await retrieveLatestBackupInfo(); + const { userID, backupID } = + await retrieveLatestBackupInfo(userIdentifier); if (currentUserInfo?.id !== userID) { throw new Error('Backup returned different userID'); @@ -209,20 +217,20 @@ } Alert.alert('Signing with previous primary Olm Account result', message); }, [ + retrieveLatestBackupInfo, + userIdentifier, currentUserInfo?.id, + identityClient, getAuthMetadata, getBackupSecret, getBackupUserKeys, - identityClient, - retrieveLatestBackupInfo, ]); const testRestoreForSIWEUser = React.useCallback(async () => { let message = 'success'; try { - const { latestBackupInfo, userIdentifier } = - await retrieveLatestBackupInfo(); - const { siweBackupData, backupID } = latestBackupInfo; + const { siweBackupData, backupID } = + await retrieveLatestBackupInfo(userIdentifier); if (!siweBackupData) { throw new Error('Missing SIWE message for Wallet user backup'); @@ -248,7 +256,7 @@ message = `Backup restore error: ${String(getMessageForException(e))}`; console.error(message); } - }, [navigation, retrieveLatestBackupInfo]); + }, [retrieveLatestBackupInfo, userIdentifier, navigation]); const onBackupToggled = React.useCallback( (value: boolean) => {