diff --git a/lib/hooks/peer-list-hooks.js b/lib/hooks/peer-list-hooks.js --- a/lib/hooks/peer-list-hooks.js +++ b/lib/hooks/peer-list-hooks.js @@ -198,6 +198,8 @@ export type CurrentIdentityUserState = { +currentDeviceList: SignedDeviceList, +currentUserPlatformDetails: UserDevicesPlatformDetails, + +userID: string, + +deviceID: string, }; function useCurrentIdentityUserState(): () => Promise { const identityContext = React.useContext(IdentityClientContext); @@ -205,9 +207,9 @@ const { identityClient, getAuthMetadata } = identityContext; return React.useCallback(async () => { - const { userID } = await getAuthMetadata(); - if (!userID) { - throw new Error('Missing userID'); + const { userID, deviceID } = await getAuthMetadata(); + if (!userID || !deviceID) { + throw new Error('Missing auth metadata'); } const { getDeviceListsForUsers } = identityClient; @@ -222,6 +224,8 @@ return { currentDeviceList, currentUserPlatformDetails, + userID, + deviceID, }; }, [getAuthMetadata, identityClient]); } 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 @@ -1,6 +1,5 @@ // @flow -import invariant from 'invariant'; import * as React from 'react'; import { createUserKeysBackupActionTypes } from 'lib/actions/backup-actions.js'; @@ -10,7 +9,6 @@ } from 'lib/hooks/peer-list-hooks.js'; import { useDeviceKind } from 'lib/hooks/primary-device-hooks.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; -import { IdentityClientContext } from 'lib/shared/identity-client-context.js'; import { useStaffAlert } from 'lib/shared/staff-utils.js'; import { getMessageForException } from 'lib/utils/errors.js'; import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js'; @@ -40,10 +38,6 @@ const startingBackupHandlerInProgress = React.useRef(false); const [handlerStarted, setHandlerStarted] = React.useState(false); - const identityContext = React.useContext(IdentityClientContext); - invariant(identityContext, 'Identity context should be set'); - const { getAuthMetadata } = identityContext; - const getCurrentIdentityUserState = useCurrentIdentityUserState(); const migrateToNewFlow = useMigrationToNewFlow(); @@ -99,16 +93,6 @@ void (async () => { backupUploadInProgress.current = true; - let userID, deviceID; - try { - const authMetadata = await getAuthMetadata(); - userID = authMetadata.userID; - deviceID = authMetadata.deviceID; - } catch (e) { - backupUploadInProgress.current = false; - return; - } - // CurrentIdentityUserState is required to check if migration to // new flow is needed. let currentIdentityUserState: ?CurrentIdentityUserState = null; @@ -149,11 +133,7 @@ // Early return without checking `shouldCreateUserKeysBackup` // is safe because migration is uploading User Keys backup. - return await migrateToNewFlow( - userID, - deviceID, - currentIdentityUserState, - ); + return await migrateToNewFlow(currentIdentityUserState); } const backupID = await createUserKeysBackup(); @@ -179,7 +159,6 @@ createUserKeysBackup, deviceKind, dispatchActionPromise, - getAuthMetadata, getCurrentIdentityUserState, handlerStarted, latestBackupInfo, diff --git a/native/backup/use-migration-to-new-flow.js b/native/backup/use-migration-to-new-flow.js --- a/native/backup/use-migration-to-new-flow.js +++ b/native/backup/use-migration-to-new-flow.js @@ -54,8 +54,6 @@ } function useMigrationToNewFlow(): ( - userID: ?string, - deviceID: ?string, currentIdentityUserState: CurrentIdentityUserState, ) => Promise { const identityContext = React.useContext(IdentityClientContext); @@ -72,14 +70,8 @@ return React.useCallback( async ( - userID: ?string, - deviceID: ?string, currentIdentityUserState: CurrentIdentityUserState, ): Promise => { - if (!userID || !deviceID) { - throw new Error('Missing auth metadata'); - } - const { updateDeviceList } = identityClient; invariant( updateDeviceList, @@ -87,13 +79,20 @@ 'Are you calling it on a non-primary device?', ); + const { + deviceID, + userID, + currentDeviceList, + currentUserPlatformDetails, + } = currentIdentityUserState; + // 1. upload UserKeys (without updating the store) let backupID = await createUserKeysBackup(); // 2. create in-memory device list (reorder and sign) const newDeviceList = await reorderAndSignDeviceList( deviceID, - rawDeviceListFromSignedList(currentIdentityUserState.currentDeviceList), + rawDeviceListFromSignedList(currentDeviceList), ); if (!userID || !userIdentifier) { @@ -106,7 +105,7 @@ payload: { deviceLists: { [userID]: newDeviceList.rawList }, usersPlatformDetails: { - [userID]: currentIdentityUserState.currentUserPlatformDetails, + [userID]: currentUserPlatformDetails, }, }, });