diff --git a/lib/actions/user-actions.js b/lib/actions/user-actions.js --- a/lib/actions/user-actions.js +++ b/lib/actions/user-actions.js @@ -8,6 +8,7 @@ useBroadcastDeviceListUpdates, useBroadcastAccountDeletion, } from '../hooks/peer-list-hooks.js'; +import { useCheckIfPrimaryDevice } from '../hooks/primary-device-hooks.js'; import type { CallSingleKeyserverEndpoint, CallSingleKeyserverEndpointOptions, @@ -96,7 +97,10 @@ import { getMessageForException } from '../utils/errors.js'; import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; import { useSelector } from '../utils/redux-utils.js'; -import { usingCommServicesAccessToken } from '../utils/services-utils.js'; +import { + usingCommServicesAccessToken, + usingRestoreFlow, +} from '../utils/services-utils.js'; import sleep from '../utils/sleep.js'; const loggedOutUserInfo: LoggedOutUserInfo = { @@ -142,13 +146,13 @@ }; type UseLogOutOptions = { - +logOutType?: 'legacy' | 'primary_device' | 'secondary_device', + +logOutType: 'legacy' | 'primary_device' | 'secondary_device', +skipIdentityLogOut?: boolean, +handleUseNewFlowResponse?: () => void, }; -function useLogOut( - options: UseLogOutOptions = {}, +function useBaseLogOut( + options: UseLogOutOptions, ): (keyserverIDs?: $ReadOnlyArray) => Promise { const client = React.useContext(IdentityClientContext); const identityClient = client?.identityClient; @@ -258,6 +262,33 @@ ); } +const legacyLogOutOptions: UseLogOutOptions = Object.freeze({ + logOutType: 'legacy', +}); +function useLogOut(): () => Promise { + const callLegacyLogOut = useBaseLogOut(legacyLogOutOptions); + const callPrimaryDeviceLogOut = usePrimaryDeviceLogOut(); + const callSecondaryDeviceLogOut = useSecondaryDeviceLogOut(); + + const checkIfPrimaryDevice = useCheckIfPrimaryDevice(); + + return React.useCallback(async () => { + if (usingRestoreFlow) { + const isPrimaryDevice = await checkIfPrimaryDevice(); + return isPrimaryDevice + ? callPrimaryDeviceLogOut() + : callSecondaryDeviceLogOut(); + } else { + return callLegacyLogOut(); + } + }, [ + callLegacyLogOut, + callPrimaryDeviceLogOut, + callSecondaryDeviceLogOut, + checkIfPrimaryDevice, + ]); +} + function useIdentityLogOut(): () => Promise { const client = React.useContext(IdentityClientContext); const identityClient = client?.identityClient; @@ -341,7 +372,7 @@ const { broadcastEphemeralMessage } = usePeerToPeerCommunication(); const foreignPeerDevices = useSelector(getForeignPeerDeviceIDs); - const logOut = useLogOut(primaryDeviceLogOutOptions); + const logOut = useBaseLogOut(primaryDeviceLogOutOptions); return React.useCallback(async () => { const { identityClient, getAuthMetadata } = identityContext; const authMetadata = await getAuthMetadata(); @@ -436,7 +467,7 @@ }); function useSecondaryDeviceLogOut(): () => Promise { - const logOut = useLogOut(secondaryDeviceLogOutOptions); + const logOut = useBaseLogOut(secondaryDeviceLogOutOptions); const sendLogoutMessage = useSendLogoutMessageToPrimaryDevice(); return React.useCallback(async () => { @@ -1500,6 +1531,7 @@ useIdentitySecondaryDeviceLogIn, useLegacyLogIn, legacyLogInActionTypes, + useBaseLogOut, useLogOut, useIdentityLogOut, usePrimaryDeviceLogOut, diff --git a/lib/tunnelbroker/use-peer-to-peer-message-handler.js b/lib/tunnelbroker/use-peer-to-peer-message-handler.js --- a/lib/tunnelbroker/use-peer-to-peer-message-handler.js +++ b/lib/tunnelbroker/use-peer-to-peer-message-handler.js @@ -8,7 +8,7 @@ import { useResendPeerToPeerMessages } from './use-resend-peer-to-peer-messages.js'; import { removePeerUsersActionType } from '../actions/aux-user-actions.js'; import { invalidateTunnelbrokerDeviceTokenActionType } from '../actions/tunnelbroker-actions.js'; -import { logOutActionTypes, useLogOut } from '../actions/user-actions.js'; +import { logOutActionTypes, useBaseLogOut } from '../actions/user-actions.js'; import { usePeerOlmSessionsCreatorContext } from '../components/peer-olm-session-creator-provider.react.js'; import { useBroadcastDeviceListUpdates, @@ -51,7 +51,10 @@ // When logout is requested by primary device, logging out of Identity Service // is already handled by the primary device -const primaryRequestLogoutOptions = Object.freeze({ skipIdentityLogOut: true }); +const primaryRequestLogoutOptions = Object.freeze({ + logOutType: 'secondary_device', + skipIdentityLogOut: true, +}); // When re-broadcasting, we want to do it only to foreign peers // to avoid a vicious circle of deletion messages sent by own devices. @@ -75,7 +78,9 @@ const dispatch = useDispatch(); const dispatchActionPromise = useDispatchActionPromise(); - const primaryDeviceRequestedLogOut = useLogOut(primaryRequestLogoutOptions); + const primaryDeviceRequestedLogOut = useBaseLogOut( + primaryRequestLogoutOptions, + ); const runDeviceListUpdate = useDeviceListUpdate(); const processDMOperation = useProcessDMOperation(); diff --git a/native/profile/profile-screen.react.js b/native/profile/profile-screen.react.js --- a/native/profile/profile-screen.react.js +++ b/native/profile/profile-screen.react.js @@ -7,7 +7,7 @@ import { logOutActionTypes, - useLogOut, + useBaseLogOut, usePrimaryDeviceLogOut, useSecondaryDeviceLogOut, } from 'lib/actions/user-actions.js'; @@ -572,11 +572,12 @@ const showVersionUnsupportedAlert = useShowVersionUnsupportedAlert(false); const logOutOptions = React.useMemo( () => ({ + logOutType: 'legacy', handleUseNewFlowResponse: showVersionUnsupportedAlert, }), [showVersionUnsupportedAlert], ); - const callLogOut = useLogOut(logOutOptions); + const callLegayLogOut = useBaseLogOut(logOutOptions); const userID = useSelector( state => state.currentUserInfo && state.currentUserInfo.id, @@ -620,7 +621,7 @@ logOutLoading={logOutLoading} colors={colors} styles={styles} - logOut={callLogOut} + logOut={callLegayLogOut} logOutPrimaryDevice={callPrimaryDeviceLogOut} logOutSecondaryDevice={callSecondaryDeviceLogOut} dispatchActionPromise={dispatchActionPromise} diff --git a/web/settings/account-settings.react.js b/web/settings/account-settings.react.js --- a/web/settings/account-settings.react.js +++ b/web/settings/account-settings.react.js @@ -5,7 +5,7 @@ import uuid from 'uuid'; import { - useLogOut, + useBaseLogOut, logOutActionTypes, useSecondaryDeviceLogOut, } from 'lib/actions/user-actions.js'; @@ -48,9 +48,12 @@ const showVersionUnsupportedModal = () => { pushModal(); }; - return { handleUseNewFlowResponse: showVersionUnsupportedModal }; + return { + logOutType: 'legacy', + handleUseNewFlowResponse: showVersionUnsupportedModal, + }; }, [pushModal]); - const sendLogoutRequest = useLogOut(logOutOptions); + const sendLegacyLogoutRequest = useBaseLogOut(logOutOptions); const sendSecondaryDeviceLogoutRequest = useSecondaryDeviceLogOut(); const dispatchActionPromise = useDispatchActionPromise(); @@ -61,10 +64,10 @@ sendSecondaryDeviceLogoutRequest(), ); } - return dispatchActionPromise(logOutActionTypes, sendLogoutRequest()); + return dispatchActionPromise(logOutActionTypes, sendLegacyLogoutRequest()); }, [ dispatchActionPromise, - sendLogoutRequest, + sendLegacyLogoutRequest, sendSecondaryDeviceLogoutRequest, ]);