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 @@ -149,8 +149,9 @@ return { currentUserInfo, preRequestUserState, keyserverIDs }; }; +type LogOutType = 'legacy' | 'primary_device' | 'secondary_device'; type UseLogOutOptions = { - +logOutType: 'legacy' | 'primary_device' | 'secondary_device', + +logOutType: LogOutType, +skipIdentityLogOut?: boolean, +handleUseNewFlowResponse?: () => void, }; @@ -303,7 +304,9 @@ ]); } -function useIdentityLogOut(): () => Promise { +function useIdentityLogOut( + logOutType: LogOutType, +): () => Promise { const client = React.useContext(IdentityClientContext); const identityClient = client?.identityClient; @@ -320,11 +323,26 @@ if (!identityClient) { throw new Error('Identity service client is not initialized'); } + + let callIdentityClientLogOut; + if (logOutType === 'primary_device') { + if (!identityClient.logOutPrimaryDevice) { + throw new Error( + 'logOutPrimaryDevice not defined. ' + + 'Are you calling it on non-primary device?', + ); + } + callIdentityClientLogOut = identityClient.logOutPrimaryDevice; + } else if (logOutType === 'secondary_device') { + callIdentityClientLogOut = identityClient.logOutSecondaryDevice; + } else { + callIdentityClientLogOut = identityClient.logOut; + } try { await promiseWithTimeout( - identityClient.logOut(), + callIdentityClientLogOut(), logoutTimeout, - 'identity log_out', + `identity ${logOutType} log_out`, ); } catch (e) { console.log(`Identity logout failed: ${getMessageForException(e) ?? ''}`); @@ -336,7 +354,12 @@ commServicesAccessToken, }, }; - }, [commServicesAccessToken, identityClient, preRequestUserState]); + }, [ + commServicesAccessToken, + identityClient, + logOutType, + preRequestUserState, + ]); } function useInvalidCSATLogOut(): () => Promise { diff --git a/lib/hooks/login-hooks.js b/lib/hooks/login-hooks.js --- a/lib/hooks/login-hooks.js +++ b/lib/hooks/login-hooks.js @@ -33,7 +33,11 @@ const inactiveStep = { step: 'inactive' }; -function useLogIn(): (identityAuthPromise: Promise) => Promise { +type LoginFlow = 'restore' | 'secondary_device' | 'legacy'; + +function useLogIn( + loginFlow: LoginFlow, +): (identityAuthPromise: Promise) => Promise { const [currentStep, setCurrentStep] = React.useState(inactiveStep); @@ -71,7 +75,15 @@ // We call identityLogOut in order to reset state if identity auth succeeds // but authoritative keyserver auth fails - const identityLogOut = useIdentityLogOut(); + let logOutType; + if (loginFlow === 'restore') { + logOutType = 'primary_device'; + } else if (loginFlow === 'secondary_device') { + logOutType = 'secondary_device'; + } else { + logOutType = 'legacy'; + } + const identityLogOut = useIdentityLogOut(logOutType); const registeringOnAuthoritativeKeyserverRef = React.useRef(false); const dispatch = useDispatch(); @@ -139,7 +151,7 @@ [identityPasswordLogIn, dispatchActionPromise], ); - const logIn = useLogIn(); + const logIn = useLogIn('legacy'); return React.useCallback( (username: string, password: string) => logIn(identityPasswordAuth(username, password)), @@ -167,7 +179,7 @@ [identityWalletLogIn, dispatchActionPromise], ); - const logIn = useLogIn(); + const logIn = useLogIn('legacy'); return React.useCallback( (walletAddress: string, siweMessage: string, siweSignature: string) => logIn(identityWalletAuth(walletAddress, siweMessage, siweSignature)), @@ -187,7 +199,7 @@ [identitySecondaryDeviceLogIn, dispatchActionPromise], ); - const logIn = useLogIn(); + const logIn = useLogIn('secondary_device'); return React.useCallback( (userID: string) => logIn(identitySecondaryDeviceAuth(userID)), [logIn, identitySecondaryDeviceAuth], diff --git a/native/account/restore.js b/native/account/restore.js --- a/native/account/restore.js +++ b/native/account/restore.js @@ -162,7 +162,7 @@ [dispatchActionPromise, restoreProtocol], ); - const logIn = useLogIn(); + const logIn = useLogIn('restore'); return React.useCallback( (userIdentifier: string, secret: string, siweSocialProof?: SignedMessage) => logIn(restoreAuth(userIdentifier, secret, siweSocialProof)),