Skip to content

Commit

Permalink
Merge pull request #31316 from lukemorawski/27168-deeplinking_not_wor…
Browse files Browse the repository at this point in the history
…king_after_logout

27168 Deeplinking not working after logout
  • Loading branch information
thienlnam authored Nov 19, 2023
2 parents 0c77cbc + c583671 commit ff4c947
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 17 deletions.
13 changes: 10 additions & 3 deletions src/SCREENS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
* This is a file containing constants for all of the screen names. In most cases, we should use the routes for
* navigation. But there are situations where we may need to access screen names directly.
*/
export default {

const PROTECTED_SCREENS = {
HOME: 'Home',
CONCIERGE: 'Concierge',
REPORT_ATTACHMENTS: 'ReportAttachments',
} as const;

export default {
...PROTECTED_SCREENS,
LOADING: 'Loading',
REPORT: 'Report',
REPORT_ATTACHMENTS: 'ReportAttachments',
NOT_FOUND: 'not-found',
TRANSITION_BETWEEN_APPS: 'TransitionBetweenApps',
VALIDATE_LOGIN: 'ValidateLogin',
CONCIERGE: 'Concierge',
SETTINGS: {
ROOT: 'Settings_Root',
PREFERENCES: 'Settings_Preferences',
Expand All @@ -28,3 +33,5 @@ export default {
DESKTOP_SIGN_IN_REDIRECT: 'DesktopSignInRedirect',
SAML_SIGN_IN: 'SAMLSignIn',
} as const;

export {PROTECTED_SCREENS};
55 changes: 54 additions & 1 deletion src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Log from '@libs/Log';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import SCREENS, {PROTECTED_SCREENS} from '@src/SCREENS';
import getStateFromPath from './getStateFromPath';
import originalGetTopmostReportActionId from './getTopmostReportActionID';
import originalGetTopmostReportId from './getTopmostReportId';
Expand Down Expand Up @@ -305,6 +305,57 @@ function setIsNavigationReady() {
resolveNavigationIsReadyPromise();
}

/**
* Checks if the navigation state contains routes that are protected (over the auth wall).
*
* @function
* @param {Object} state - react-navigation state object
*
* @returns {Boolean}
*/
function navContainsProtectedRoutes(state) {
if (!state || !state.routeNames || !_.isArray(state.routeNames)) {
return false;
}

const protectedScreensName = _.values(PROTECTED_SCREENS);
const difference = _.difference(protectedScreensName, state.routeNames);

return !difference.length;
}

/**
* Waits for the navitgation state to contain protected routes specified in PROTECTED_SCREENS constant.
* If the navigation is in a state, where protected routes are avilable, the promise resolve immediately.
*
* @function
* @returns {Promise<void>} A promise that resolves when the one of the PROTECTED_SCREENS screen is available in the nav tree.
*
* @example
* waitForProtectedRoutes()
* .then(()=> console.log('Protected routes are present!'))
*/
function waitForProtectedRoutes() {
return new Promise((resolve) => {
isNavigationReady().then(() => {
const currentState = navigationRef.current.getState();
if (navContainsProtectedRoutes(currentState)) {
resolve();
return;
}
let unsubscribe;
const handleStateChange = ({data}) => {
const state = lodashGet(data, 'state');
if (navContainsProtectedRoutes(state)) {
unsubscribe();
resolve();
}
};
unsubscribe = navigationRef.current.addListener('state', handleStateChange);
});
});
}

export default {
setShouldPopAllStateOnUP,
canNavigate,
Expand All @@ -320,6 +371,8 @@ export default {
getTopmostReportId,
getRouteNameFromStateEvent,
getTopmostReportActionId,
waitForProtectedRoutes,
navContainsProtectedRoutes,
};

export {navigationRef};
25 changes: 12 additions & 13 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as Pusher from '@libs/Pusher/pusher';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import SidebarUtils from '@libs/SidebarUtils';
import * as UserUtils from '@libs/UserUtils';
import Visibility from '@libs/Visibility';
import CONFIG from '@src/CONFIG';
Expand Down Expand Up @@ -1986,7 +1985,6 @@ function toggleEmojiReaction(reportID, reportAction, reactionObject, existingRea
* @param {Boolean} isAuthenticated
*/
function openReportFromDeepLink(url, isAuthenticated) {
const route = ReportUtils.getRouteFromLink(url);
const reportID = ReportUtils.getReportIDFromLink(url);

if (reportID && !isAuthenticated) {
Expand All @@ -2004,18 +2002,19 @@ function openReportFromDeepLink(url, isAuthenticated) {

// Navigate to the report after sign-in/sign-up.
InteractionManager.runAfterInteractions(() => {
SidebarUtils.isSidebarLoadedReady().then(() => {
if (route === ROUTES.CONCIERGE) {
navigateToConciergeChat(true);
return;
}
if (Session.isAnonymousUser() && !Session.canAccessRouteByAnonymousUser(route)) {
Navigation.isNavigationReady().then(() => {
Session.waitForUserSignIn().then(() => {
Navigation.waitForProtectedRoutes().then(() => {
const route = ReportUtils.getRouteFromLink(url);
if (route === ROUTES.CONCIERGE) {
navigateToConciergeChat(true);
return;
}
if (Session.isAnonymousUser() && !Session.canAccessRouteByAnonymousUser(route)) {
Session.signOutAndRedirectToSignIn();
});
return;
}
Navigation.navigate(route, CONST.NAVIGATION.ACTION_TYPE.PUSH);
return;
}
Navigation.navigate(route, CONST.NAVIGATION.ACTION_TYPE.PUSH);
});
});
});
}
Expand Down

0 comments on commit ff4c947

Please sign in to comment.