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 @@ -141,6 +141,7 @@ type UseLogOutOptions = { +logOutType?: 'legacy' | 'primary_device' | 'secondary_device', +skipIdentityLogOut?: boolean, + +handleUseNewFlowResponse?: () => void, }; function useLogOut( @@ -151,6 +152,7 @@ const preRequestUserState = usePreRequestUserState(); const callKeyserverLogOut = useKeyserverCall(keyserverLogOut); + const sendLogoutMessage = useSendLogoutMessageToPrimaryDevice(); const removeAllHolders = useClearAllHolders(); @@ -158,7 +160,7 @@ state => state.commServicesAccessToken, ); - const { logOutType, skipIdentityLogOut } = options; + const { logOutType, skipIdentityLogOut, handleUseNewFlowResponse } = options; return React.useCallback( async (keyserverIDs?: $ReadOnlyArray) => { const holdersPromise = (async () => { @@ -187,11 +189,28 @@ ); } callIdentityClientLogOut = identityClient.logOutPrimaryDevice; + } else if (logOutType === 'secondary_device') { + callIdentityClientLogOut = identityClient.logOutSecondaryDevice; } else { - callIdentityClientLogOut = - logOutType === 'secondary_device' - ? identityClient.logOutSecondaryDevice - : identityClient.logOut; + callIdentityClientLogOut = async () => { + try { + await identityClient.logOut(); + } catch (e) { + const errorMessage = getMessageForException(e); + if (errorMessage !== 'use_new_flow') { + throw e; + } + + // When use_new_flow is returned on legacy V1 login, + // we are sure that another device upgraded flows and has become + // primary, so this is now a secondary device which still uses + // old flow + try { + await sendLogoutMessage(); + } catch {} + handleUseNewFlowResponse?.(); + } + }; } try { await Promise.race([ @@ -228,6 +247,8 @@ preRequestUserState, removeAllHolders, skipIdentityLogOut, + sendLogoutMessage, + handleUseNewFlowResponse, ], ); }