Skip to content

Commit c48fd3e

Browse files
authored
Merge pull request #60487 from callstack-internal/fix/20121-fix-magic-link-error
fix: fix problem with redirecting to desktop
2 parents d04ecfc + c7ca914 commit c48fd3e

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/components/DeeplinkWrapper/index.website.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function isMacOSWeb(): boolean {
2020
return !isMobile() && typeof navigator === 'object' && typeof navigator.userAgent === 'string' && /Mac/i.test(navigator.userAgent) && !/Electron/i.test(navigator.userAgent);
2121
}
2222

23-
function promptToOpenInDesktopApp(initialUrl = '') {
23+
function promptToOpenInDesktopApp(currentUserAccountID?: number, initialUrl = '') {
2424
// If the current url path is /transition..., meaning it was opened from oldDot, during this transition period:
2525
// 1. The user session may not exist, because sign-in has not been completed yet.
2626
// 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,16 +35,24 @@ function promptToOpenInDesktopApp(initialUrl = '') {
3535
} else {
3636
// Match any magic link (/v/<account id>/<6 digit code>)
3737
const isMagicLink = CONST.REGEX.ROUTES.VALIDATE_LOGIN.test(window.location.pathname);
38+
const shouldAuthenticateWithCurrentAccount = !isMagicLink || (isMagicLink && !!currentUserAccountID && window.location.pathname.includes(currentUserAccountID.toString()));
3839

39-
beginDeepLinkRedirect(!isMagicLink, getInternalNewExpensifyPath(initialUrl));
40+
beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount, isMagicLink, getInternalNewExpensifyPath(initialUrl));
4041
}
4142
}
4243

4344
function DeeplinkWrapper({children, isAuthenticated, autoAuthState, initialUrl}: DeeplinkWrapperProps) {
4445
const [currentScreen, setCurrentScreen] = useState<string | undefined>();
4546
const [hasShownPrompt, setHasShownPrompt] = useState(false);
4647
const removeListener = useRef<() => void>();
47-
const [isActingAsDelegate] = useOnyx(ONYXKEYS.ACCOUNT, {selector: (account) => !!account?.delegatedAccess?.delegate});
48+
const [isActingAsDelegate] = useOnyx(ONYXKEYS.ACCOUNT, {
49+
selector: (account) => !!account?.delegatedAccess?.delegate,
50+
canBeMissing: true,
51+
});
52+
const [currentUserAccountID] = useOnyx(ONYXKEYS.SESSION, {
53+
selector: (session) => session?.accountID,
54+
canBeMissing: true,
55+
});
4856
const isActingAsDelegateRef = useRef(isActingAsDelegate);
4957
const delegatorEmailRef = useRef(getSearchParamFromUrl(getCurrentUrl(), 'delegatorEmail'));
5058

@@ -95,7 +103,7 @@ function DeeplinkWrapper({children, isAuthenticated, autoAuthState, initialUrl}:
95103
// Otherwise, we want to wait until the navigation state is set up
96104
// and we know the user is on a screen that supports deeplinks.
97105
if (isAuthenticated) {
98-
promptToOpenInDesktopApp(initialUrl);
106+
promptToOpenInDesktopApp(currentUserAccountID, initialUrl);
99107
setHasShownPrompt(true);
100108
} else {
101109
// 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}:
111119
promptToOpenInDesktopApp();
112120
setHasShownPrompt(true);
113121
}
114-
}, [currentScreen, hasShownPrompt, isAuthenticated, autoAuthState, initialUrl]);
122+
}, [currentScreen, hasShownPrompt, isAuthenticated, autoAuthState, initialUrl, currentUserAccountID]);
115123

116124
return children;
117125
}

src/libs/actions/App.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ function redirectThirdPartyDesktopSignIn() {
534534
/**
535535
* @param shouldAuthenticateWithCurrentAccount Optional, indicates whether default authentication method (shortLivedAuthToken) should be used
536536
*/
537-
function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, initialRoute?: string) {
537+
function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, isMagicLink?: boolean, initialRoute?: string) {
538538
// There's no support for anonymous users on desktop
539539
if (isAnonymousUser()) {
540540
return;
@@ -560,7 +560,7 @@ function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, init
560560
return;
561561
}
562562

563-
Browser.openRouteInDesktopApp(response.shortLivedAuthToken, currentUserEmail, initialRoute);
563+
Browser.openRouteInDesktopApp(response.shortLivedAuthToken, currentUserEmail, isMagicLink ? '/r' : initialRoute);
564564
});
565565
}
566566

0 commit comments

Comments
 (0)