Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for redirecting user to a target URL when they click on the magic link code #36390

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ type PublicScreensParamList = {
[SCREENS.VALIDATE_LOGIN]: {
accountID: string;
validateCode: string;
exitTo?: Routes | HybridAppRoute;
};
[SCREENS.UNLINK_LOGIN]: {
accountID?: string;
Expand Down
29 changes: 23 additions & 6 deletions src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import throttle from 'lodash/throttle';
import type {ChannelAuthorizationData} from 'pusher-js/types/src/core/auth/options';
import type {ChannelAuthorizationCallback} from 'pusher-js/with-encryption';
import {Linking} from 'react-native';
import {InteractionManager, Linking, NativeModules} from 'react-native';
import type {OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
Expand Down Expand Up @@ -45,6 +45,7 @@ import * as Welcome from '@userActions/Welcome';
import CONFIG from '@src/CONFIG';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {HybridAppRoute, Route as Routes} from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import type Credentials from '@src/types/onyx/Credentials';
Expand Down Expand Up @@ -504,11 +505,6 @@ function signInWithValidateCode(accountID: number, code: string, twoFactorAuthCo
});
}

function signInWithValidateCodeAndNavigate(accountID: number, validateCode: string, twoFactorAuthCode = '') {
signInWithValidateCode(accountID, validateCode, twoFactorAuthCode);
Navigation.navigate(ROUTES.HOME);
}

/**
* Initializes the state of the automatic authentication when the user clicks on a magic link.
*
Expand Down Expand Up @@ -855,6 +851,26 @@ function waitForUserSignIn(): Promise<boolean> {
});
}

function handleExitToNavigation(exitTo: Routes | HybridAppRoute) {
InteractionManager.runAfterInteractions(() => {
waitForUserSignIn().then(() => {
Navigation.waitForProtectedRoutes().then(() => {
const url = NativeModules.HybridAppModule ? Navigation.parseHybridAppUrl(exitTo) : exitTo;
Navigation.navigate(url, CONST.NAVIGATION.TYPE.FORCED_UP);
});
});
});
}

function signInWithValidateCodeAndNavigate(accountID: number, validateCode: string, twoFactorAuthCode = '', exitTo?: Routes | HybridAppRoute) {
signInWithValidateCode(accountID, validateCode, twoFactorAuthCode);
if (exitTo) {
handleExitToNavigation(exitTo);
} else {
Navigation.navigate(ROUTES.HOME);
}
}

/**
* check if the route can be accessed by anonymous user
*
Expand Down Expand Up @@ -890,6 +906,7 @@ export {
checkIfActionIsAllowed,
signIn,
signInWithValidateCode,
handleExitToNavigation,
signInWithValidateCodeAndNavigate,
initAutoAuthState,
signInWithShortLivedAuthToken,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/ValidateLoginPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {ValidateLoginPageOnyxNativeProps, ValidateLoginPageProps} from './t

function ValidateLoginPage({
route: {
params: {accountID, validateCode},
params: {accountID, validateCode, exitTo},
},
session,
}: ValidateLoginPageProps<ValidateLoginPageOnyxNativeProps>) {
Expand All @@ -21,7 +21,7 @@ function ValidateLoginPage({
// because we don't want to block the user with the interstitial page.
Navigation.goBack();
} else {
Session.signInWithValidateCodeAndNavigate(Number(accountID), validateCode);
Session.signInWithValidateCodeAndNavigate(Number(accountID), validateCode, '', exitTo);
}
});
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
10 changes: 8 additions & 2 deletions src/pages/ValidateLoginPage/index.website.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function ValidateLoginPage({
account,
credentials,
route: {
params: {accountID, validateCode},
params: {accountID, validateCode, exitTo},
},
session,
}: ValidateLoginPageProps<ValidateLoginPageOnyxProps>) {
Expand All @@ -35,6 +35,9 @@ function ValidateLoginPage({
Session.initAutoAuthState(autoAuthState);

if (isSignedIn || !login) {
if (exitTo) {
Session.handleExitToNavigation(exitTo);
}
return;
}

Expand All @@ -45,14 +48,17 @@ function ValidateLoginPage({

useEffect(() => {
if (!!login || !cachedAccountID || !is2FARequired) {
if (exitTo) {
Session.handleExitToNavigation(exitTo);
}
return;
}

// The user clicked the option to sign in the current tab
Navigation.isNavigationReady().then(() => {
Navigation.goBack();
});
}, [login, cachedAccountID, is2FARequired]);
}, [login, cachedAccountID, is2FARequired, exitTo]);

return (
<>
Expand Down
Loading