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 @@ -144,6 +144,7 @@ type UseLogOutOptions = { +logOutType?: 'legacy' | 'primary_device' | 'secondary_device', +skipIdentityLogOut?: boolean, + +handleUseNewFlowResponse?: () => void, }; function useLogOut( @@ -154,6 +155,7 @@ const preRequestUserState = usePreRequestUserState(); const callKeyserverLogOut = useKeyserverCall(keyserverLogOut); + const sendLogoutMessage = useSendLogoutMessageToPrimaryDevice(false); const removeAllHolders = useClearAllHolders(); @@ -161,7 +163,7 @@ state => state.commServicesAccessToken, ); - const { logOutType, skipIdentityLogOut } = options; + const { logOutType, skipIdentityLogOut, handleUseNewFlowResponse } = options; return React.useCallback( async (keyserverIDs?: $ReadOnlyArray) => { const holdersPromise = (async () => { @@ -190,11 +192,30 @@ ); } 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 (err) { + console.log('Failed to send logout message:', err); + } + handleUseNewFlowResponse?.(); + } + }; } try { await Promise.race([ @@ -231,6 +252,8 @@ preRequestUserState, removeAllHolders, skipIdentityLogOut, + sendLogoutMessage, + handleUseNewFlowResponse, ], ); }