Skip to content

Commit

Permalink
Merge pull request Expensify#36390 from rayane-djouah/add-support-for…
Browse files Browse the repository at this point in the history
…-redirecting-user-to-a-target-url-when-they-click-on-the-magic-link-code

Add support for redirecting user to a target URL when they click on the magic link code
  • Loading branch information
techievivek authored Feb 14, 2024
2 parents 6dd67ec + 643df64 commit 00fc955
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,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

0 comments on commit 00fc955

Please sign in to comment.