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 @@ -35,7 +35,10 @@ permissionsAndAuthRelatedRequestTimeout, callIdentityServiceTimeout, } from '../shared/timeouts.js'; -import { usePeerToPeerCommunication } from '../tunnelbroker/peer-to-peer-context.js'; +import { + PeerToPeerContext, + usePeerToPeerCommunication, +} from '../tunnelbroker/peer-to-peer-context.js'; import type { LegacyLogInInfo, LegacyLogInResult, @@ -358,20 +361,24 @@ ]); } -const secondaryDeviceLogOutOptions = Object.freeze({ - logOutType: 'secondary_device', -}); - -function useSecondaryDeviceLogOut(): () => Promise { - const logOut = useLogOut(secondaryDeviceLogOutOptions); - +function useSendLogoutMessageToPrimaryDevice( + mandatory?: boolean = true, +): () => Promise { const identityContext = React.useContext(IdentityClientContext); if (!identityContext) { throw new Error('Identity service client is not initialized'); } - const { broadcastEphemeralMessage } = usePeerToPeerCommunication(); + const peerToPeerContext = React.useContext(PeerToPeerContext); return React.useCallback(async () => { + if (!peerToPeerContext) { + if (mandatory) { + throw new Error('PeerToPeerContext not set'); + } + return; + } + + const { broadcastEphemeralMessage } = peerToPeerContext; const { identityClient, getAuthMetadata } = identityContext; const authMetadata = await getAuthMetadata(); const { userID, deviceID } = authMetadata; @@ -398,10 +405,23 @@ [recipient], authMetadata, ); + }, [identityContext, peerToPeerContext, mandatory]); +} + +const secondaryDeviceLogOutOptions = Object.freeze({ + logOutType: 'secondary_device', +}); + +function useSecondaryDeviceLogOut(): () => Promise { + const logOut = useLogOut(secondaryDeviceLogOutOptions); + const sendLogoutMessage = useSendLogoutMessageToPrimaryDevice(); + + return React.useCallback(async () => { + await sendLogoutMessage(); // log out of identity service, keyserver and visually return logOut(); - }, [identityContext, broadcastEphemeralMessage, logOut]); + }, [sendLogoutMessage, logOut]); } const claimUsernameActionTypes = Object.freeze({ diff --git a/lib/tunnelbroker/peer-to-peer-context.js b/lib/tunnelbroker/peer-to-peer-context.js --- a/lib/tunnelbroker/peer-to-peer-context.js +++ b/lib/tunnelbroker/peer-to-peer-context.js @@ -447,4 +447,4 @@ return context; } -export { PeerToPeerProvider, usePeerToPeerCommunication }; +export { PeerToPeerContext, PeerToPeerProvider, usePeerToPeerCommunication };