From d0e2fc33ced48fa9a123804ce1221c1a1da18a98 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 17 Aug 2023 16:26:03 +0200 Subject: [PATCH 1/7] fix: added navigation to every route after user log in when opening app from deeplink --- src/libs/actions/Report.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 8b898a6aaaea..c07f78a77126 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1770,6 +1770,7 @@ function openReportFromDeepLink(url, isAuthenticated) { if (route === ROUTES.CONCIERGE) { navigateToConciergeChat(); } + Navigation.navigate(route, CONST.NAVIGATION.TYPE.UP); }); }); } From 5e135c5a7b6c7821703c0cf7e271b67a4eada654 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 17 Aug 2023 16:47:05 +0200 Subject: [PATCH 2/7] fix: changed navigation type --- src/libs/actions/Report.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index c07f78a77126..bf4effbb6b62 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1770,7 +1770,7 @@ function openReportFromDeepLink(url, isAuthenticated) { if (route === ROUTES.CONCIERGE) { navigateToConciergeChat(); } - Navigation.navigate(route, CONST.NAVIGATION.TYPE.UP); + Navigation.navigate(route, CONST.NAVIGATION.TYPE.PUSH); }); }); } From 809088173f59d67b02b47e6f7c243823454c1c1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20M=C3=B3rawski?= Date: Tue, 22 Aug 2023 17:38:09 +0200 Subject: [PATCH 3/7] refactor/waiting for user to sign in --- src/libs/actions/Report.js | 4 ++-- src/libs/actions/Session/index.js | 39 ++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index bf4effbb6b62..441b5d6e5897 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -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({ @@ -1763,7 +1763,7 @@ function openReportFromDeepLink(url, isAuthenticated) { // Navigate to the report after sign-in/sign-up. InteractionManager.runAfterInteractions(() => { - SidebarUtils.isSidebarLoadedReady().then(() => { + Session.waitForUserSingIn().then(() => { if (reportID) { Navigation.navigate(ROUTES.getReportRoute(reportID), CONST.NAVIGATION.TYPE.UP); } diff --git a/src/libs/actions/Session/index.js b/src/libs/actions/Session/index.js index aa5ff229267f..eaf3dd0345bb 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -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 sessionAuthToken = null; +let authPromiseResolver = null; + Onyx.connect({ key: ONYXKEYS.SESSION, - callback: (session) => (authTokenType = lodashGet(session, 'authTokenType')), + callback: (session) => { + authTokenType = lodashGet(session, 'authTokenType'); + sessionAuthToken = lodashGet(session, 'authToken'); + + if (sessionAuthToken && authPromiseResolver) { + authPromiseResolver(true); + authPromiseResolver = null; + } + }, }); let credentials = {}; @@ -75,7 +85,7 @@ function signOutAndRedirectToSignIn() { Linking.getInitialURL().then((url) => { const reportID = ReportUtils.getReportIDFromLink(url); if (reportID) { - Report.setLastOpenedPublicRoom(reportID); + Onyx.set(ONYXKEYS.LAST_OPENED_PUBLIC_ROOM_ID, reportID); } }); } @@ -749,6 +759,28 @@ 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. + * + * @returns {Promise} A promise that resolves to `true` once the user is signed in. + * @example + * waitForUserSignIn().then(() => { + * console.log('User is signed in!'); + * }); + */ +function waitForUserSingIn() { + return new Promise((resolve) => { + if (sessionAuthToken) { + resolve(true); + } else { + authPromiseResolver = resolve; + } + }); +} + export { beginSignIn, beginAppleSignIn, @@ -776,4 +808,5 @@ export { isAnonymousUser, toggleTwoFactorAuth, validateTwoFactorAuth, + waitForUserSingIn, }; From 6e01b62535ec5638e218ea8f3ef967b34192b653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20M=C3=B3rawski?= Date: Wed, 23 Aug 2023 12:36:56 +0200 Subject: [PATCH 4/7] fix/function name typo --- src/libs/actions/Report.js | 2 +- src/libs/actions/Session/index.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 441b5d6e5897..da8e4fb838c0 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1763,7 +1763,7 @@ function openReportFromDeepLink(url, isAuthenticated) { // Navigate to the report after sign-in/sign-up. InteractionManager.runAfterInteractions(() => { - Session.waitForUserSingIn().then(() => { + Session.waitForUserSignIn().then(() => { if (reportID) { Navigation.navigate(ROUTES.getReportRoute(reportID), CONST.NAVIGATION.TYPE.UP); } diff --git a/src/libs/actions/Session/index.js b/src/libs/actions/Session/index.js index eaf3dd0345bb..f3289c5a652a 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -764,6 +764,7 @@ function validateTwoFactorAuth(twoFactorAuthCode) { * * 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} A promise that resolves to `true` once the user is signed in. * @example @@ -771,7 +772,7 @@ function validateTwoFactorAuth(twoFactorAuthCode) { * console.log('User is signed in!'); * }); */ -function waitForUserSingIn() { +function waitForUserSignIn() { return new Promise((resolve) => { if (sessionAuthToken) { resolve(true); @@ -808,5 +809,5 @@ export { isAnonymousUser, toggleTwoFactorAuth, validateTwoFactorAuth, - waitForUserSingIn, + waitForUserSignIn, }; From c68830f0e28a3ec29a05f1825365bebc513e9d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20M=C3=B3rawski?= Date: Wed, 23 Aug 2023 12:49:17 +0200 Subject: [PATCH 5/7] var name change for naming consitency --- src/libs/actions/Session/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Session/index.js b/src/libs/actions/Session/index.js index f3289c5a652a..56167872be11 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -22,14 +22,14 @@ import * as ErrorUtils from '../../ErrorUtils'; import * as ReportUtils from '../../ReportUtils'; 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'); + sessionAuthTokenType = lodashGet(session, 'authTokenType'); sessionAuthToken = lodashGet(session, 'authToken'); if (sessionAuthToken && authPromiseResolver) { @@ -71,7 +71,7 @@ function signOut() { * @return {boolean} */ function isAnonymousUser() { - return authTokenType === 'anonymousAccount'; + return sessionAuthTokenType === 'anonymousAccount'; } function signOutAndRedirectToSignIn() { From 0e75aa9e23dbcc880752e17e67c8ad6b81d7f7b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20M=C3=B3rawski?= Date: Wed, 23 Aug 2023 12:53:38 +0200 Subject: [PATCH 6/7] change from set to Onyx.merge --- src/libs/actions/Session/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Session/index.js b/src/libs/actions/Session/index.js index 56167872be11..8a8aabf6e8f4 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -85,7 +85,7 @@ function signOutAndRedirectToSignIn() { Linking.getInitialURL().then((url) => { const reportID = ReportUtils.getReportIDFromLink(url); if (reportID) { - Onyx.set(ONYXKEYS.LAST_OPENED_PUBLIC_ROOM_ID, reportID); + Onyx.merge(ONYXKEYS.LAST_OPENED_PUBLIC_ROOM_ID, reportID); } }); } From 39516dff546b9e7c3dc57625e4a67f3144eaf6f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20M=C3=B3rawski?= Date: Wed, 23 Aug 2023 15:45:32 +0200 Subject: [PATCH 7/7] added early returns for Report navigation cases --- src/libs/actions/Report.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index da8e4fb838c0..b0aba835ac8e 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1766,9 +1766,11 @@ function openReportFromDeepLink(url, isAuthenticated) { 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); });