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 3 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
6 changes: 0 additions & 6 deletions src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,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 @@ -890,7 +885,6 @@ export {
checkIfActionIsAllowed,
signIn,
signInWithValidateCode,
signInWithValidateCodeAndNavigate,
initAutoAuthState,
signInWithShortLivedAuthToken,
cleanupSession,
Expand Down
31 changes: 28 additions & 3 deletions src/pages/ValidateLoginPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React, {useEffect} from 'react';
import {InteractionManager, NativeModules} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import Navigation from '@libs/Navigation/Navigation';
import * as Session from '@userActions/Session';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {ValidateLoginPageOnyxNativeProps, ValidateLoginPageProps} from './types';

function ValidateLoginPage({
route: {
params: {accountID, validateCode},
params: {accountID, validateCode, exitTo},
},
session,
}: ValidateLoginPageProps<ValidateLoginPageOnyxNativeProps>) {
Expand All @@ -19,9 +21,32 @@ function ValidateLoginPage({
if (session?.authToken) {
// If already signed in, do not show the validate code if not on web,
// because we don't want to block the user with the interstitial page.
Navigation.goBack();
if (exitTo) {
InteractionManager.runAfterInteractions(() => {
Session.waitForUserSignIn().then(() => {
Navigation.waitForProtectedRoutes().then(() => {
const url = NativeModules.HybridAppModule ? Navigation.parseHybridAppUrl(exitTo) : exitTo;
Navigation.navigate(url, CONST.NAVIGATION.TYPE.FORCED_UP);
});
});
rayane-djouah marked this conversation as resolved.
Show resolved Hide resolved
});
} else {
Navigation.goBack();
}
} else {
Session.signInWithValidateCodeAndNavigate(Number(accountID), validateCode);
Session.signInWithValidateCode(Number(accountID), validateCode);
if (exitTo) {
InteractionManager.runAfterInteractions(() => {
Session.waitForUserSignIn().then(() => {
Navigation.waitForProtectedRoutes().then(() => {
const url = NativeModules.HybridAppModule ? Navigation.parseHybridAppUrl(exitTo) : exitTo;
Navigation.navigate(url, CONST.NAVIGATION.TYPE.FORCED_UP);
});
});
});
} else {
Navigation.navigate(ROUTES.HOME);
}
}
});
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
27 changes: 25 additions & 2 deletions src/pages/ValidateLoginPage/index.website.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, {useEffect} from 'react';
import {InteractionManager, NativeModules} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import ExpiredValidateCodeModal from '@components/ValidateCode/ExpiredValidateCodeModal';
Expand All @@ -14,7 +15,7 @@ function ValidateLoginPage({
account,
credentials,
route: {
params: {accountID, validateCode},
params: {accountID, validateCode, exitTo},
},
session,
}: ValidateLoginPageProps<ValidateLoginPageOnyxProps>) {
Expand All @@ -27,14 +28,26 @@ function ValidateLoginPage({
useEffect(() => {
if (!login && isSignedIn && (autoAuthState === CONST.AUTO_AUTH_STATE.SIGNING_IN || autoAuthState === CONST.AUTO_AUTH_STATE.JUST_SIGNED_IN)) {
// The user clicked the option to sign in the current tab

Navigation.isNavigationReady().then(() => {
Navigation.goBack();
});
return;
}

Session.initAutoAuthState(autoAuthState);

if (isSignedIn || !login) {
if (exitTo) {
InteractionManager.runAfterInteractions(() => {
Session.waitForUserSignIn().then(() => {
Navigation.waitForProtectedRoutes().then(() => {
const url = NativeModules.HybridAppModule ? Navigation.parseHybridAppUrl(exitTo) : exitTo;
Navigation.navigate(url, CONST.NAVIGATION.TYPE.FORCED_UP);
});
});
});
}
return;
}

Expand All @@ -45,14 +58,24 @@ function ValidateLoginPage({

useEffect(() => {
if (!!login || !cachedAccountID || !is2FARequired) {
if (exitTo) {
InteractionManager.runAfterInteractions(() => {
Session.waitForUserSignIn().then(() => {
Navigation.waitForProtectedRoutes().then(() => {
const url = NativeModules.HybridAppModule ? Navigation.parseHybridAppUrl(exitTo) : exitTo;
Navigation.navigate(url, CONST.NAVIGATION.TYPE.FORCED_UP);
});
});
});
}
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