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

Fix/deeplinks when app in background #26073

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 @@ -1763,13 +1763,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we are doing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes , according to onyx documentation set should be used only when deleting the key from store or resetting object or array of data. In our case we are only storing primitive value so to that case better is to use merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we are removing Report.setLastOpenedPublicRoom(reportID) because it was causing circular dependency

}
});
}
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,
};