Skip to content

Commit

Permalink
Merge pull request #26073 from kubabutkiewicz/fix/deeplinks-when-app-…
Browse files Browse the repository at this point in the history
…in-background-v2

Fix/deeplinks when app in background
  • Loading branch information
Hayata Suenaga authored Sep 12, 2023
2 parents 86f8720 + 29e4440 commit b7a0046
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import * as ErrorUtils from '../ErrorUtils';
import * as UserUtils from '../UserUtils';
import * as Welcome from './Welcome';
import * as PersonalDetailsUtils from '../PersonalDetailsUtils';
import SidebarUtils from '../SidebarUtils';
import * as OptionsListUtils from '../OptionsListUtils';
import * as Environment from '../Environment/Environment';
import * as Session from './Session';

let currentUserAccountID;
Onyx.connect({
Expand Down Expand Up @@ -1759,13 +1759,16 @@ function openReportFromDeepLink(url, isAuthenticated) {

// Navigate to the report after sign-in/sign-up.
InteractionManager.runAfterInteractions(() => {
SidebarUtils.isSidebarLoadedReady().then(() => {
Session.waitForUserSignIn().then(() => {
if (reportID) {
Navigation.navigate(ROUTES.getReportRoute(reportID), CONST.NAVIGATION.TYPE.UP);
return;
}
if (route === ROUTES.CONCIERGE) {
navigateToConciergeChat();
return;
}
Navigation.navigate(route, CONST.NAVIGATION.TYPE.PUSH);
});
});
}
Expand Down
44 changes: 39 additions & 5 deletions src/libs/actions/Session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ import * as Device from '../Device';
import ROUTES from '../../../ROUTES';
import * as ErrorUtils from '../../ErrorUtils';
import * as ReportUtils from '../../ReportUtils';
import * as Report from '../Report';
import {hideContextMenu} from '../../../pages/home/report/ContextMenu/ReportActionContextMenu';

let authTokenType = '';
let sessionAuthTokenType = '';
let sessionAuthToken = null;
let authPromiseResolver = null;

Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (session) => (authTokenType = lodashGet(session, 'authTokenType')),
callback: (session) => {
sessionAuthTokenType = lodashGet(session, 'authTokenType');
sessionAuthToken = lodashGet(session, 'authToken');

if (sessionAuthToken && authPromiseResolver) {
authPromiseResolver(true);
authPromiseResolver = null;
}
},
});

let credentials = {};
Expand Down Expand Up @@ -61,7 +71,7 @@ function signOut() {
* @return {boolean}
*/
function isAnonymousUser() {
return authTokenType === 'anonymousAccount';
return sessionAuthTokenType === 'anonymousAccount';
}

function signOutAndRedirectToSignIn() {
Expand All @@ -75,7 +85,7 @@ function signOutAndRedirectToSignIn() {
Linking.getInitialURL().then((url) => {
const reportID = ReportUtils.getReportIDFromLink(url);
if (reportID) {
Report.setLastOpenedPublicRoom(reportID);
Onyx.merge(ONYXKEYS.LAST_OPENED_PUBLIC_ROOM_ID, reportID);
}
});
}
Expand Down Expand Up @@ -749,6 +759,29 @@ function validateTwoFactorAuth(twoFactorAuthCode) {
API.write('TwoFactorAuth_Validate', {twoFactorAuthCode}, {optimisticData, successData, failureData});
}

/**
* Waits for a user to sign in.
*
* If the user is already signed in (`authToken` is truthy), the promise resolves immediately.
* Otherwise, the promise will resolve when the `authToken` in `ONYXKEYS.SESSION` becomes truthy via the Onyx callback.
* The promise will not reject on failed login attempt.
*
* @returns {Promise<boolean>} A promise that resolves to `true` once the user is signed in.
* @example
* waitForUserSignIn().then(() => {
* console.log('User is signed in!');
* });
*/
function waitForUserSignIn() {
return new Promise((resolve) => {
if (sessionAuthToken) {
resolve(true);
} else {
authPromiseResolver = resolve;
}
});
}

export {
beginSignIn,
beginAppleSignIn,
Expand Down Expand Up @@ -776,4 +809,5 @@ export {
isAnonymousUser,
toggleTwoFactorAuth,
validateTwoFactorAuth,
waitForUserSignIn,
};

0 comments on commit b7a0046

Please sign in to comment.