diff --git a/src/components/DeeplinkWrapper/index.website.tsx b/src/components/DeeplinkWrapper/index.website.tsx index 91ce5e10febd..3ffb3eb73b4c 100644 --- a/src/components/DeeplinkWrapper/index.website.tsx +++ b/src/components/DeeplinkWrapper/index.website.tsx @@ -20,7 +20,7 @@ function isMacOSWeb(): boolean { return !isMobile() && typeof navigator === 'object' && typeof navigator.userAgent === 'string' && /Mac/i.test(navigator.userAgent) && !/Electron/i.test(navigator.userAgent); } -function promptToOpenInDesktopApp(initialUrl = '') { +function promptToOpenInDesktopApp(currentUserAccountID?: number, initialUrl = '') { // If the current url path is /transition..., meaning it was opened from oldDot, during this transition period: // 1. The user session may not exist, because sign-in has not been completed yet. // 2. There may be non-idempotent operations (e.g. create a new workspace), which obviously should not be executed again in the desktop app. @@ -35,8 +35,9 @@ function promptToOpenInDesktopApp(initialUrl = '') { } else { // Match any magic link (/v//<6 digit code>) const isMagicLink = CONST.REGEX.ROUTES.VALIDATE_LOGIN.test(window.location.pathname); + const shouldAuthenticateWithCurrentAccount = !isMagicLink || (isMagicLink && !!currentUserAccountID && window.location.pathname.includes(currentUserAccountID.toString())); - beginDeepLinkRedirect(!isMagicLink, getInternalNewExpensifyPath(initialUrl)); + beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount, isMagicLink, getInternalNewExpensifyPath(initialUrl)); } } @@ -44,7 +45,14 @@ function DeeplinkWrapper({children, isAuthenticated, autoAuthState, initialUrl}: const [currentScreen, setCurrentScreen] = useState(); const [hasShownPrompt, setHasShownPrompt] = useState(false); const removeListener = useRef<() => void>(); - const [isActingAsDelegate] = useOnyx(ONYXKEYS.ACCOUNT, {selector: (account) => !!account?.delegatedAccess?.delegate}); + const [isActingAsDelegate] = useOnyx(ONYXKEYS.ACCOUNT, { + selector: (account) => !!account?.delegatedAccess?.delegate, + canBeMissing: true, + }); + const [currentUserAccountID] = useOnyx(ONYXKEYS.SESSION, { + selector: (session) => session?.accountID, + canBeMissing: true, + }); const isActingAsDelegateRef = useRef(isActingAsDelegate); const delegatorEmailRef = useRef(getSearchParamFromUrl(getCurrentUrl(), 'delegatorEmail')); @@ -95,7 +103,7 @@ function DeeplinkWrapper({children, isAuthenticated, autoAuthState, initialUrl}: // Otherwise, we want to wait until the navigation state is set up // and we know the user is on a screen that supports deeplinks. if (isAuthenticated) { - promptToOpenInDesktopApp(initialUrl); + promptToOpenInDesktopApp(currentUserAccountID, initialUrl); setHasShownPrompt(true); } else { // Navigation state is not set up yet, we're unsure if we should show the deep link prompt or not @@ -111,7 +119,7 @@ function DeeplinkWrapper({children, isAuthenticated, autoAuthState, initialUrl}: promptToOpenInDesktopApp(); setHasShownPrompt(true); } - }, [currentScreen, hasShownPrompt, isAuthenticated, autoAuthState, initialUrl]); + }, [currentScreen, hasShownPrompt, isAuthenticated, autoAuthState, initialUrl, currentUserAccountID]); return children; } diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts index 739513e110c0..54ef5c2de6bb 100644 --- a/src/libs/actions/App.ts +++ b/src/libs/actions/App.ts @@ -534,7 +534,7 @@ function redirectThirdPartyDesktopSignIn() { /** * @param shouldAuthenticateWithCurrentAccount Optional, indicates whether default authentication method (shortLivedAuthToken) should be used */ -function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, initialRoute?: string) { +function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, isMagicLink?: boolean, initialRoute?: string) { // There's no support for anonymous users on desktop if (isAnonymousUser()) { return; @@ -560,7 +560,7 @@ function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, init return; } - Browser.openRouteInDesktopApp(response.shortLivedAuthToken, currentUserEmail, initialRoute); + Browser.openRouteInDesktopApp(response.shortLivedAuthToken, currentUserEmail, isMagicLink ? '/r' : initialRoute); }); }