diff --git a/lib/components/secondary-device-qr-auth-context-provider.react.js b/lib/components/secondary-device-qr-auth-context-provider.react.js --- a/lib/components/secondary-device-qr-auth-context-provider.react.js +++ b/lib/components/secondary-device-qr-auth-context-provider.react.js @@ -42,13 +42,15 @@ type SecondaryDeviceQRAuthContextType = { +qrData: QRData, - +generateQRCode: () => Promise, + +openSecondaryQRAuth: () => Promise, + +closeSecondaryQRAuth: () => void, }; const SecondaryDeviceQRAuthContext: React.Context = React.createContext({ qrData: null, - generateQRCode: async () => {}, + openSecondaryQRAuth: async () => {}, + closeSecondaryQRAuth: () => {}, }); function SecondaryDeviceQRAuthContextProvider(props: Props): React.Node { @@ -75,7 +77,7 @@ const identityContext = React.useContext(IdentityClientContext); const identityClient = identityContext?.identityClient; - const generateQRCode = React.useCallback(async () => { + const openSecondaryQRAuth = React.useCallback(async () => { try { const [ed25519, rawAESKey] = await Promise.all([ getContentSigningKey(), @@ -91,6 +93,10 @@ } }, [generateAESKey, setUnauthorizedDeviceID]); + const closeSecondaryQRAuth = React.useCallback(() => { + setUnauthorizedDeviceID(null); + }, [setUnauthorizedDeviceID]); + const logInSecondaryDevice = useSecondaryDeviceLogIn(); const performLogIn = React.useCallback( async (userID: string) => { @@ -98,10 +104,10 @@ await logInSecondaryDevice(userID); } catch (err) { onLogInError(err); - void generateQRCode(); + void openSecondaryQRAuth(); } }, - [logInSecondaryDevice, onLogInError, generateQRCode], + [logInSecondaryDevice, onLogInError, openSecondaryQRAuth], ); React.useEffect(() => { @@ -212,9 +218,10 @@ const value = React.useMemo( () => ({ qrData, - generateQRCode, + openSecondaryQRAuth, + closeSecondaryQRAuth, }), - [qrData, generateQRCode], + [qrData, openSecondaryQRAuth, closeSecondaryQRAuth], ); return ( diff --git a/native/account/qr-code-screen.react.js b/native/account/qr-code-screen.react.js --- a/native/account/qr-code-screen.react.js +++ b/native/account/qr-code-screen.react.js @@ -1,5 +1,6 @@ // @flow +import { useFocusEffect } from '@react-navigation/core'; import * as React from 'react'; import { View, Text, useWindowDimensions } from 'react-native'; import QRCode from 'react-native-qrcode-svg'; @@ -25,11 +26,15 @@ }; function QRCodeScreen(props: QRCodeScreenProps): React.Node { - const { qrData, generateQRCode } = useSecondaryDeviceQRAuthContext(); + const { qrData, openSecondaryQRAuth, closeSecondaryQRAuth } = + useSecondaryDeviceQRAuthContext(); - React.useEffect(() => { - void generateQRCode(); - }, [generateQRCode]); + useFocusEffect( + React.useCallback(() => { + void openSecondaryQRAuth(); + return closeSecondaryQRAuth; + }, [closeSecondaryQRAuth, openSecondaryQRAuth]), + ); const { platform } = getConfig().platformDetails; const qrCodeURL = React.useMemo(() => { diff --git a/web/account/qr-code-login.react.js b/web/account/qr-code-login.react.js --- a/web/account/qr-code-login.react.js +++ b/web/account/qr-code-login.react.js @@ -11,11 +11,13 @@ import css from './qr-code-login.css'; function QRCodeLogin(): React.Node { - const { qrData, generateQRCode } = useSecondaryDeviceQRAuthContext(); + const { qrData, openSecondaryQRAuth, closeSecondaryQRAuth } = + useSecondaryDeviceQRAuthContext(); React.useEffect(() => { - void generateQRCode(); - }, [generateQRCode]); + void openSecondaryQRAuth(); + return closeSecondaryQRAuth; + }, [closeSecondaryQRAuth, openSecondaryQRAuth]); const { platform } = getConfig().platformDetails; const qrCodeURL = React.useMemo(() => {