From 7bef47b80a824e59e5f25a0a2479526ca40e7756 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 21 Sep 2021 13:28:30 -1000 Subject: [PATCH 1/8] Move cleanup logic to callback --- src/libs/actions/SignInRedirect.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 94bf78df5a0d..a6e43ca3c0c2 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -1,3 +1,4 @@ +import lodashGet from 'lodash/get'; import Onyx from 'react-native-onyx'; import ONYXKEYS from '../../ONYXKEYS'; import * as Pusher from '../Pusher/pusher'; @@ -19,6 +20,24 @@ Onyx.connect({ callback: val => currentPreferredLocale = val, }); +let previousAuthToken; +Onyx.connect({ + key: ONYXKEYS.SESSION, + callback: (val) => { + const newAuthToken = lodashGet(val, 'authToken'); + if (previousAuthToken && !newAuthToken) { + // We got signed out in this tab or another so clean up any subscriptions and timers + UnreadIndicatorUpdater.stopListeningForReportChanges(); + PushNotification.deregister(); + PushNotification.clearNotifications(); + Pusher.disconnect(); + Timers.clearAll(); + } + previousAuthToken = newAuthToken; + }, +}); + + /** * Clears the Onyx store and redirects to the sign in page. * Normally this method would live in Session.js, but that would cause a circular dependency with Network.js. @@ -26,12 +45,6 @@ Onyx.connect({ * @param {String} [errorMessage] error message to be displayed on the sign in page */ function redirectToSignIn(errorMessage) { - UnreadIndicatorUpdater.stopListeningForReportChanges(); - PushNotification.deregister(); - PushNotification.clearNotifications(); - Pusher.disconnect(); - Timers.clearAll(); - const activeClients = currentActiveClients; const preferredLocale = currentPreferredLocale; From 3a55a943132e7c6840259b0f44705da3103d6677 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 21 Sep 2021 13:29:55 -1000 Subject: [PATCH 2/8] remove newline --- src/libs/actions/SignInRedirect.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index a6e43ca3c0c2..eeef3ba56bac 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -37,7 +37,6 @@ Onyx.connect({ }, }); - /** * Clears the Onyx store and redirects to the sign in page. * Normally this method would live in Session.js, but that would cause a circular dependency with Network.js. From e89394d55a7d5c0f99738b90ddc42f2d2d10be7d Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Mon, 27 Sep 2021 16:26:55 -1000 Subject: [PATCH 3/8] add SignoutManager to manage cross tab communication --- package-lock.json | 65 ++++++++++++++++++++++++- package.json | 1 + src/libs/SignoutManager/index.js | 30 ++++++++++++ src/libs/SignoutManager/index.native.js | 6 +++ src/libs/actions/SignInRedirect.js | 33 ++++++------- 5 files changed, 116 insertions(+), 19 deletions(-) create mode 100644 src/libs/SignoutManager/index.js create mode 100644 src/libs/SignoutManager/index.native.js diff --git a/package-lock.json b/package-lock.json index 21e93841fed9..3a2f758e3c68 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19445,6 +19445,36 @@ } } }, + "broadcast-channel": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/broadcast-channel/-/broadcast-channel-4.2.0.tgz", + "integrity": "sha512-XX9yNnIy/v2T+HR5EKIH7ziM2mYTefsrzZ2lkCmFZxwCDG7Ns7HLIooUXA114sCeFRJ4MEXEgdMapxfPDh9Xkg==", + "requires": { + "@babel/runtime": "^7.7.2", + "detect-node": "^2.1.0", + "js-sha3": "0.8.0", + "microseconds": "0.2.0", + "nano-time": "1.0.0", + "oblivious-set": "1.0.0", + "rimraf": "3.0.2", + "unload": "2.2.0" + }, + "dependencies": { + "detect-node": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.1.0.tgz", + "integrity": "sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==" + }, + "rimraf": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", + "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==", + "requires": { + "glob": "^7.1.3" + } + } + } + }, "brorand": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/brorand/-/brorand-1.1.0.tgz", @@ -22138,8 +22168,7 @@ "detect-node": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.0.4.tgz", - "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==", - "dev": true + "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==" }, "detect-port-alt": { "version": "1.1.6", @@ -32057,6 +32086,11 @@ "resolved": "https://registry.npmjs.org/jquery/-/jquery-3.6.0.tgz", "integrity": "sha512-JVzAR/AjBvVt2BmYhxRCSYysDsPcssdmTFnzyLEts9qNwmjmu4JTAMYubEfwVOSwpQ1I1sKKFcxhZCI2buerfw==" }, + "js-sha3": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.8.0.tgz", + "integrity": "sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==" + }, "js-string-escape": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/js-string-escape/-/js-string-escape-1.0.1.tgz", @@ -34661,6 +34695,11 @@ "to-regex": "^3.0.2" } }, + "microseconds": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/microseconds/-/microseconds-0.2.0.tgz", + "integrity": "sha512-n7DHHMjR1avBbSpsTBj6fmMGh2AGrifVV4e+WYc3Q9lO+xnSZ3NyhcBND3vzzatt05LFhoKFRxrIyklmLlUtyA==" + }, "miller-rabin": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/miller-rabin/-/miller-rabin-4.0.1.tgz", @@ -34984,6 +35023,14 @@ "integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==", "optional": true }, + "nano-time": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/nano-time/-/nano-time-1.0.0.tgz", + "integrity": "sha1-sFVPaa2J4i0JB/ehKwmTpdlhN+8=", + "requires": { + "big-integer": "^1.6.16" + } + }, "nanoid": { "version": "3.1.23", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.23.tgz", @@ -35560,6 +35607,11 @@ "integrity": "sha512-eJJDYkhJFFbBBAxeh8xW+weHlkI28n2ZdQV/J/DNfWfSKlGEf2xcfAbZTv3riEXHAhL9SVOTs2pRmXiSTf78xg==", "dev": true }, + "oblivious-set": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/oblivious-set/-/oblivious-set-1.0.0.tgz", + "integrity": "sha512-z+pI07qxo4c2CulUHCDf9lcqDlMSo72N/4rLUpRXf6fu+q8vjt8y0xS+Tlf8NTJDdTXHbdeO1n3MlbctwEoXZw==" + }, "obuf": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/obuf/-/obuf-1.1.2.tgz", @@ -43440,6 +43492,15 @@ "resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz", "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==" }, + "unload": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/unload/-/unload-2.2.0.tgz", + "integrity": "sha512-B60uB5TNBLtN6/LsgAf3udH9saB5p7gqJwcFfbOEZ8BcBHnGwCf6G/TGiEqkRAxX7zAFIUtzdrXQSdL3Q/wqNA==", + "requires": { + "@babel/runtime": "^7.6.2", + "detect-node": "^2.0.4" + } + }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/package.json b/package.json index 4e3989b28a17..1498ca785419 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "@react-navigation/native": "^6.0.0-next.13", "@react-navigation/stack": "^6.0.0-next.25", "babel-plugin-transform-remove-console": "^6.9.4", + "broadcast-channel": "^4.2.0", "dotenv": "^8.2.0", "electron-context-menu": "^2.3.0", "electron-log": "^4.3.5", diff --git a/src/libs/SignoutManager/index.js b/src/libs/SignoutManager/index.js new file mode 100644 index 000000000000..bb1f4374893c --- /dev/null +++ b/src/libs/SignoutManager/index.js @@ -0,0 +1,30 @@ +import {BroadcastChannel} from 'broadcast-channel'; + +let signoutCallback = () => {}; +const SIGNOUT_EVENT = 'signout'; + +/** + * @param {Function} callback + */ +function onSignout(callback) { + signoutCallback = callback; +} + +// Setup channel to listen across tabs +const signoutChannel = new BroadcastChannel('signout'); +signoutChannel.addEventListener('message', (message) => { + if (message !== SIGNOUT_EVENT) { + return; + } + + signoutCallback(); +}); + +function notifyTabsOfSignout() { + signoutChannel.postMessage(SIGNOUT_EVENT); +} + +export default { + notifyTabsOfSignout, + onSignout, +}; diff --git a/src/libs/SignoutManager/index.native.js b/src/libs/SignoutManager/index.native.js new file mode 100644 index 000000000000..e12c991e5cc0 --- /dev/null +++ b/src/libs/SignoutManager/index.native.js @@ -0,0 +1,6 @@ +// Native does not need to worry about cross tab communication + +export default { + notifyTabsOfSignout: () => {}, + onSignout: () => {}, +}; diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index eeef3ba56bac..5a8394a8e975 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -1,10 +1,10 @@ -import lodashGet from 'lodash/get'; import Onyx from 'react-native-onyx'; import ONYXKEYS from '../../ONYXKEYS'; import * as Pusher from '../Pusher/pusher'; import UnreadIndicatorUpdater from '../UnreadIndicatorUpdater'; import PushNotification from '../Notification/PushNotification'; import Timers from '../Timers'; +import SignoutManager from '../SignoutManager'; let currentActiveClients; Onyx.connect({ @@ -20,22 +20,19 @@ Onyx.connect({ callback: val => currentPreferredLocale = val, }); -let previousAuthToken; -Onyx.connect({ - key: ONYXKEYS.SESSION, - callback: (val) => { - const newAuthToken = lodashGet(val, 'authToken'); - if (previousAuthToken && !newAuthToken) { - // We got signed out in this tab or another so clean up any subscriptions and timers - UnreadIndicatorUpdater.stopListeningForReportChanges(); - PushNotification.deregister(); - PushNotification.clearNotifications(); - Pusher.disconnect(); - Timers.clearAll(); - } - previousAuthToken = newAuthToken; - }, -}); +/** + * Put any logic that needs to run when we are signed out here. This can be triggered when the current tab or another tab signs out. + */ +function cleanupSession() { + // We got signed out in this tab or another so clean up any subscriptions and timers + UnreadIndicatorUpdater.stopListeningForReportChanges(); + PushNotification.deregister(); + PushNotification.clearNotifications(); + Pusher.disconnect(); + Timers.clearAll(); +} + +SignoutManager.onSignout(cleanupSession); /** * Clears the Onyx store and redirects to the sign in page. @@ -46,6 +43,8 @@ Onyx.connect({ function redirectToSignIn(errorMessage) { const activeClients = currentActiveClients; const preferredLocale = currentPreferredLocale; + cleanupSession(); + SignoutManager.notifyTabsOfSignout(); // Clearing storage discards the authToken. This causes a redirect to the SignIn screen Onyx.clear() From 99890c714154f59c32daf9ef4d5f88ad6fcd765c Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Mon, 27 Sep 2021 16:34:51 -1000 Subject: [PATCH 4/8] move session cleanup tasks to AuthScreens --- .../Navigation/AppNavigator/AuthScreens.js | 3 +- src/libs/SignoutManager/index.js | 30 ------------------- src/libs/SignoutManager/index.native.js | 6 ---- src/libs/actions/Session.js | 19 +++++++++++- src/libs/actions/SignInRedirect.js | 21 ------------- 5 files changed, 20 insertions(+), 59 deletions(-) delete mode 100644 src/libs/SignoutManager/index.js delete mode 100644 src/libs/SignoutManager/index.native.js diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 5338f4cb283d..5b3f49ea3267 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -70,6 +70,7 @@ import CardOverlay from '../../../components/CardOverlay'; import defaultScreenOptions from './defaultScreenOptions'; import * as API from '../../API'; import {setLocale} from '../../actions/App'; +import {cleanupSession} from '../../actions/Session'; Onyx.connect({ key: ONYXKEYS.MY_PERSONAL_DETAILS, @@ -249,7 +250,7 @@ class AuthScreens extends React.Component { if (this.unsubscribeGroupShortcut) { this.unsubscribeGroupShortcut(); } - NetworkConnection.stopListeningForReconnect(); + cleanupSession(); clearInterval(this.interval); this.interval = null; hasLoadedPolicies = false; diff --git a/src/libs/SignoutManager/index.js b/src/libs/SignoutManager/index.js deleted file mode 100644 index bb1f4374893c..000000000000 --- a/src/libs/SignoutManager/index.js +++ /dev/null @@ -1,30 +0,0 @@ -import {BroadcastChannel} from 'broadcast-channel'; - -let signoutCallback = () => {}; -const SIGNOUT_EVENT = 'signout'; - -/** - * @param {Function} callback - */ -function onSignout(callback) { - signoutCallback = callback; -} - -// Setup channel to listen across tabs -const signoutChannel = new BroadcastChannel('signout'); -signoutChannel.addEventListener('message', (message) => { - if (message !== SIGNOUT_EVENT) { - return; - } - - signoutCallback(); -}); - -function notifyTabsOfSignout() { - signoutChannel.postMessage(SIGNOUT_EVENT); -} - -export default { - notifyTabsOfSignout, - onSignout, -}; diff --git a/src/libs/SignoutManager/index.native.js b/src/libs/SignoutManager/index.native.js deleted file mode 100644 index e12c991e5cc0..000000000000 --- a/src/libs/SignoutManager/index.native.js +++ /dev/null @@ -1,6 +0,0 @@ -// Native does not need to worry about cross tab communication - -export default { - notifyTabsOfSignout: () => {}, - onSignout: () => {}, -}; diff --git a/src/libs/actions/Session.js b/src/libs/actions/Session.js index 9629b613b7ad..588dcb5e07ac 100644 --- a/src/libs/actions/Session.js +++ b/src/libs/actions/Session.js @@ -13,7 +13,10 @@ import Navigation from '../Navigation/Navigation'; import ROUTES from '../../ROUTES'; import {translateLocal} from '../translate'; import * as Network from '../Network'; - +import UnreadIndicatorUpdater from '../UnreadIndicatorUpdater'; +import Timers from '../Timers'; +import * as Pusher from '../Pusher/pusher'; +import NetworkConnection from '../NetworkConnection'; let credentials = {}; Onyx.connect({ @@ -306,6 +309,19 @@ function continueSessionFromECom(accountID, validateCode, twoFactorAuthCode) { }); } +/** + * Put any logic that needs to run when we are signed out here. This can be triggered when the current tab or another tab signs out. + */ +function cleanupSession() { + // We got signed out in this tab or another so clean up any subscriptions and timers + NetworkConnection.stopListeningForReconnect(); + UnreadIndicatorUpdater.stopListeningForReportChanges(); + PushNotification.deregister(); + PushNotification.clearNotifications(); + Pusher.disconnect(); + Timers.clearAll(); +} + export { continueSessionFromECom, fetchAccountDetails, @@ -315,4 +331,5 @@ export { reopenAccount, resendValidationLink, resetPassword, + cleanupSession, }; diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 5a8394a8e975..45a4350d360a 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -1,10 +1,5 @@ import Onyx from 'react-native-onyx'; import ONYXKEYS from '../../ONYXKEYS'; -import * as Pusher from '../Pusher/pusher'; -import UnreadIndicatorUpdater from '../UnreadIndicatorUpdater'; -import PushNotification from '../Notification/PushNotification'; -import Timers from '../Timers'; -import SignoutManager from '../SignoutManager'; let currentActiveClients; Onyx.connect({ @@ -20,20 +15,6 @@ Onyx.connect({ callback: val => currentPreferredLocale = val, }); -/** - * Put any logic that needs to run when we are signed out here. This can be triggered when the current tab or another tab signs out. - */ -function cleanupSession() { - // We got signed out in this tab or another so clean up any subscriptions and timers - UnreadIndicatorUpdater.stopListeningForReportChanges(); - PushNotification.deregister(); - PushNotification.clearNotifications(); - Pusher.disconnect(); - Timers.clearAll(); -} - -SignoutManager.onSignout(cleanupSession); - /** * Clears the Onyx store and redirects to the sign in page. * Normally this method would live in Session.js, but that would cause a circular dependency with Network.js. @@ -43,8 +24,6 @@ SignoutManager.onSignout(cleanupSession); function redirectToSignIn(errorMessage) { const activeClients = currentActiveClients; const preferredLocale = currentPreferredLocale; - cleanupSession(); - SignoutManager.notifyTabsOfSignout(); // Clearing storage discards the authToken. This causes a redirect to the SignIn screen Onyx.clear() From db5a0b35c3760a8033415c81dfd5dcd54228f8d8 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Mon, 27 Sep 2021 16:39:18 -1000 Subject: [PATCH 5/8] remove broadcast channel. not necessary --- package-lock.json | 65 ++--------------------------------------------- package.json | 1 - 2 files changed, 2 insertions(+), 64 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3a2f758e3c68..21e93841fed9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19445,36 +19445,6 @@ } } }, - "broadcast-channel": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/broadcast-channel/-/broadcast-channel-4.2.0.tgz", - "integrity": "sha512-XX9yNnIy/v2T+HR5EKIH7ziM2mYTefsrzZ2lkCmFZxwCDG7Ns7HLIooUXA114sCeFRJ4MEXEgdMapxfPDh9Xkg==", - "requires": { - "@babel/runtime": "^7.7.2", - "detect-node": "^2.1.0", - "js-sha3": "0.8.0", - "microseconds": "0.2.0", - "nano-time": "1.0.0", - "oblivious-set": "1.0.0", - "rimraf": "3.0.2", - "unload": "2.2.0" - }, - "dependencies": { - "detect-node": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.1.0.tgz", - "integrity": "sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==" - }, - "rimraf": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", - "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==", - "requires": { - "glob": "^7.1.3" - } - } - } - }, "brorand": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/brorand/-/brorand-1.1.0.tgz", @@ -22168,7 +22138,8 @@ "detect-node": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.0.4.tgz", - "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==" + "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==", + "dev": true }, "detect-port-alt": { "version": "1.1.6", @@ -32086,11 +32057,6 @@ "resolved": "https://registry.npmjs.org/jquery/-/jquery-3.6.0.tgz", "integrity": "sha512-JVzAR/AjBvVt2BmYhxRCSYysDsPcssdmTFnzyLEts9qNwmjmu4JTAMYubEfwVOSwpQ1I1sKKFcxhZCI2buerfw==" }, - "js-sha3": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.8.0.tgz", - "integrity": "sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==" - }, "js-string-escape": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/js-string-escape/-/js-string-escape-1.0.1.tgz", @@ -34695,11 +34661,6 @@ "to-regex": "^3.0.2" } }, - "microseconds": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/microseconds/-/microseconds-0.2.0.tgz", - "integrity": "sha512-n7DHHMjR1avBbSpsTBj6fmMGh2AGrifVV4e+WYc3Q9lO+xnSZ3NyhcBND3vzzatt05LFhoKFRxrIyklmLlUtyA==" - }, "miller-rabin": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/miller-rabin/-/miller-rabin-4.0.1.tgz", @@ -35023,14 +34984,6 @@ "integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==", "optional": true }, - "nano-time": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/nano-time/-/nano-time-1.0.0.tgz", - "integrity": "sha1-sFVPaa2J4i0JB/ehKwmTpdlhN+8=", - "requires": { - "big-integer": "^1.6.16" - } - }, "nanoid": { "version": "3.1.23", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.23.tgz", @@ -35607,11 +35560,6 @@ "integrity": "sha512-eJJDYkhJFFbBBAxeh8xW+weHlkI28n2ZdQV/J/DNfWfSKlGEf2xcfAbZTv3riEXHAhL9SVOTs2pRmXiSTf78xg==", "dev": true }, - "oblivious-set": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/oblivious-set/-/oblivious-set-1.0.0.tgz", - "integrity": "sha512-z+pI07qxo4c2CulUHCDf9lcqDlMSo72N/4rLUpRXf6fu+q8vjt8y0xS+Tlf8NTJDdTXHbdeO1n3MlbctwEoXZw==" - }, "obuf": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/obuf/-/obuf-1.1.2.tgz", @@ -43492,15 +43440,6 @@ "resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz", "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==" }, - "unload": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/unload/-/unload-2.2.0.tgz", - "integrity": "sha512-B60uB5TNBLtN6/LsgAf3udH9saB5p7gqJwcFfbOEZ8BcBHnGwCf6G/TGiEqkRAxX7zAFIUtzdrXQSdL3Q/wqNA==", - "requires": { - "@babel/runtime": "^7.6.2", - "detect-node": "^2.0.4" - } - }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/package.json b/package.json index 1498ca785419..4e3989b28a17 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "@react-navigation/native": "^6.0.0-next.13", "@react-navigation/stack": "^6.0.0-next.25", "babel-plugin-transform-remove-console": "^6.9.4", - "broadcast-channel": "^4.2.0", "dotenv": "^8.2.0", "electron-context-menu": "^2.3.0", "electron-log": "^4.3.5", From 7da6e7467c9e63ae962c51ec585e12546dba7da8 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Mon, 27 Sep 2021 17:38:50 -1000 Subject: [PATCH 6/8] Add BroadcastChannel as we cant use localStorage --- package-lock.json | 65 ++++++++++++++++++- package.json | 1 + src/components/SignoutManager/index.js | 43 ++++++++++++ src/components/SignoutManager/index.native.js | 20 ++++++ src/libs/actions/SignInRedirect.js | 41 +++++++----- 5 files changed, 150 insertions(+), 20 deletions(-) create mode 100644 src/components/SignoutManager/index.js create mode 100644 src/components/SignoutManager/index.native.js diff --git a/package-lock.json b/package-lock.json index 12a8967f2da4..6bf3924adc31 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19437,6 +19437,36 @@ } } }, + "broadcast-channel": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/broadcast-channel/-/broadcast-channel-4.2.0.tgz", + "integrity": "sha512-XX9yNnIy/v2T+HR5EKIH7ziM2mYTefsrzZ2lkCmFZxwCDG7Ns7HLIooUXA114sCeFRJ4MEXEgdMapxfPDh9Xkg==", + "requires": { + "@babel/runtime": "^7.7.2", + "detect-node": "^2.1.0", + "js-sha3": "0.8.0", + "microseconds": "0.2.0", + "nano-time": "1.0.0", + "oblivious-set": "1.0.0", + "rimraf": "3.0.2", + "unload": "2.2.0" + }, + "dependencies": { + "detect-node": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.1.0.tgz", + "integrity": "sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==" + }, + "rimraf": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", + "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==", + "requires": { + "glob": "^7.1.3" + } + } + } + }, "brorand": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/brorand/-/brorand-1.1.0.tgz", @@ -22139,8 +22169,7 @@ "detect-node": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.0.4.tgz", - "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==", - "dev": true + "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==" }, "detect-port-alt": { "version": "1.1.6", @@ -32058,6 +32087,11 @@ "resolved": "https://registry.npmjs.org/jquery/-/jquery-3.6.0.tgz", "integrity": "sha512-JVzAR/AjBvVt2BmYhxRCSYysDsPcssdmTFnzyLEts9qNwmjmu4JTAMYubEfwVOSwpQ1I1sKKFcxhZCI2buerfw==" }, + "js-sha3": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.8.0.tgz", + "integrity": "sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==" + }, "js-string-escape": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/js-string-escape/-/js-string-escape-1.0.1.tgz", @@ -34662,6 +34696,11 @@ "to-regex": "^3.0.2" } }, + "microseconds": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/microseconds/-/microseconds-0.2.0.tgz", + "integrity": "sha512-n7DHHMjR1avBbSpsTBj6fmMGh2AGrifVV4e+WYc3Q9lO+xnSZ3NyhcBND3vzzatt05LFhoKFRxrIyklmLlUtyA==" + }, "miller-rabin": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/miller-rabin/-/miller-rabin-4.0.1.tgz", @@ -34985,6 +35024,14 @@ "integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==", "optional": true }, + "nano-time": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/nano-time/-/nano-time-1.0.0.tgz", + "integrity": "sha1-sFVPaa2J4i0JB/ehKwmTpdlhN+8=", + "requires": { + "big-integer": "^1.6.16" + } + }, "nanoid": { "version": "3.1.25", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.25.tgz", @@ -35561,6 +35608,11 @@ "integrity": "sha512-eJJDYkhJFFbBBAxeh8xW+weHlkI28n2ZdQV/J/DNfWfSKlGEf2xcfAbZTv3riEXHAhL9SVOTs2pRmXiSTf78xg==", "dev": true }, + "oblivious-set": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/oblivious-set/-/oblivious-set-1.0.0.tgz", + "integrity": "sha512-z+pI07qxo4c2CulUHCDf9lcqDlMSo72N/4rLUpRXf6fu+q8vjt8y0xS+Tlf8NTJDdTXHbdeO1n3MlbctwEoXZw==" + }, "obuf": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/obuf/-/obuf-1.1.2.tgz", @@ -43436,6 +43488,15 @@ "resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz", "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==" }, + "unload": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/unload/-/unload-2.2.0.tgz", + "integrity": "sha512-B60uB5TNBLtN6/LsgAf3udH9saB5p7gqJwcFfbOEZ8BcBHnGwCf6G/TGiEqkRAxX7zAFIUtzdrXQSdL3Q/wqNA==", + "requires": { + "@babel/runtime": "^7.6.2", + "detect-node": "^2.0.4" + } + }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/package.json b/package.json index 0ec54bb83410..c8da72348db4 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "@react-navigation/native": "6.0.2", "@react-navigation/stack": "6.0.7", "babel-plugin-transform-remove-console": "^6.9.4", + "broadcast-channel": "^4.2.0", "dotenv": "^8.2.0", "electron-context-menu": "^2.3.0", "electron-log": "^4.3.5", diff --git a/src/components/SignoutManager/index.js b/src/components/SignoutManager/index.js new file mode 100644 index 000000000000..7d0e6cbdd39f --- /dev/null +++ b/src/components/SignoutManager/index.js @@ -0,0 +1,43 @@ + +import {BroadcastChannel} from 'broadcast-channel'; + +const SIGNOUT_EVENT = 'signout'; +const SIGNOUT_CHANNEL = 'signout_channel'; + +const channel = new BroadcastChannel(SIGNOUT_CHANNEL); +let signoutCallback = () => {}; + +channel.addEventListener('message', ({event, errorMessage}) => { + if (event !== SIGNOUT_EVENT) { + return; + } + + signoutCallback(errorMessage); +}); + +/** + * @param {Function} callback + */ +function registerSignoutCallback(callback) { + signoutCallback = callback; +} + +/** + * @param {String} errorMessage + */ +function notifyTabsOfSignout(errorMessage) { + channel.postMessage({event: SIGNOUT_EVENT, errorMessage}); +} + +/** + * @param {String} errorMessage + */ +function signOut(errorMessage) { + signoutCallback(errorMessage) + .then(() => notifyTabsOfSignout(errorMessage)); +} + +export default { + signOut, + registerSignoutCallback, +}; diff --git a/src/components/SignoutManager/index.native.js b/src/components/SignoutManager/index.native.js new file mode 100644 index 000000000000..8a11332308ee --- /dev/null +++ b/src/components/SignoutManager/index.native.js @@ -0,0 +1,20 @@ +let signoutCallback = () => {}; + +/** + * @param {Function} callback + */ +function registerSignoutCallback(callback) { + signoutCallback = callback; +} + +/** + * @param {String} errorMessage + */ +function signOut(errorMessage) { + signoutCallback(errorMessage); +} + +export default { + signOut, + registerSignoutCallback, +}; diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 45a4350d360a..3b2f36558a16 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -1,4 +1,5 @@ import Onyx from 'react-native-onyx'; +import SignoutManager from '../../components/SignoutManager'; import ONYXKEYS from '../../ONYXKEYS'; let currentActiveClients; @@ -16,37 +17,41 @@ Onyx.connect({ }); /** - * Clears the Onyx store and redirects to the sign in page. - * Normally this method would live in Session.js, but that would cause a circular dependency with Network.js. - * - * @param {String} [errorMessage] error message to be displayed on the sign in page + * @param {String} errorMessage + * @returns {Promise} */ -function redirectToSignIn(errorMessage) { +function clearStorageAndRedirect(errorMessage) { const activeClients = currentActiveClients; const preferredLocale = currentPreferredLocale; // Clearing storage discards the authToken. This causes a redirect to the SignIn screen - Onyx.clear() + return Onyx.clear() .then(() => { + const promises = []; + if (preferredLocale) { - Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); + promises.push(Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale)); } if (activeClients && activeClients.length > 0) { - Onyx.set(ONYXKEYS.ACTIVE_CLIENTS, activeClients); - } - - const session = { - // We must set the authToken to null so that signOut action is triggered across other clients - authToken: null, - }; - - if (errorMessage) { - session.error = errorMessage; + promises.push(Onyx.set(ONYXKEYS.ACTIVE_CLIENTS, activeClients)); } // `Onyx.clear` reinitialize the Onyx instance with initial values so use `Onyx.merge` instead of `Onyx.set`. - Onyx.merge(ONYXKEYS.SESSION, session); + promises.push(Onyx.merge(ONYXKEYS.SESSION, {error: errorMessage})); + return Promise.all(promises); }); } +SignoutManager.registerSignoutCallback(clearStorageAndRedirect); + +/** + * Clears the Onyx store and redirects to the sign in page. + * Normally this method would live in Session.js, but that would cause a circular dependency with Network.js. + * + * @param {String} [errorMessage] error message to be displayed on the sign in page + */ +function redirectToSignIn(errorMessage) { + SignoutManager.signOut(errorMessage); +} + export default redirectToSignIn; From f3e3f827fd951cbed254e2e051b0f41c593195ad Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 28 Sep 2021 06:20:51 -1000 Subject: [PATCH 7/8] add SignoutManager + remove bcast channel again --- package-lock.json | 65 +------------------ package.json | 1 - src/ONYXKEYS.js | 3 + src/components/SignoutManager/index.js | 43 ------------ src/components/SignoutManager/index.native.js | 20 ------ src/libs/SignoutManager.js | 38 +++++++++++ src/libs/actions/SignInRedirect.js | 2 +- 7 files changed, 44 insertions(+), 128 deletions(-) delete mode 100644 src/components/SignoutManager/index.js delete mode 100644 src/components/SignoutManager/index.native.js create mode 100644 src/libs/SignoutManager.js diff --git a/package-lock.json b/package-lock.json index 6bf3924adc31..12a8967f2da4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19437,36 +19437,6 @@ } } }, - "broadcast-channel": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/broadcast-channel/-/broadcast-channel-4.2.0.tgz", - "integrity": "sha512-XX9yNnIy/v2T+HR5EKIH7ziM2mYTefsrzZ2lkCmFZxwCDG7Ns7HLIooUXA114sCeFRJ4MEXEgdMapxfPDh9Xkg==", - "requires": { - "@babel/runtime": "^7.7.2", - "detect-node": "^2.1.0", - "js-sha3": "0.8.0", - "microseconds": "0.2.0", - "nano-time": "1.0.0", - "oblivious-set": "1.0.0", - "rimraf": "3.0.2", - "unload": "2.2.0" - }, - "dependencies": { - "detect-node": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.1.0.tgz", - "integrity": "sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==" - }, - "rimraf": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", - "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==", - "requires": { - "glob": "^7.1.3" - } - } - } - }, "brorand": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/brorand/-/brorand-1.1.0.tgz", @@ -22169,7 +22139,8 @@ "detect-node": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.0.4.tgz", - "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==" + "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==", + "dev": true }, "detect-port-alt": { "version": "1.1.6", @@ -32087,11 +32058,6 @@ "resolved": "https://registry.npmjs.org/jquery/-/jquery-3.6.0.tgz", "integrity": "sha512-JVzAR/AjBvVt2BmYhxRCSYysDsPcssdmTFnzyLEts9qNwmjmu4JTAMYubEfwVOSwpQ1I1sKKFcxhZCI2buerfw==" }, - "js-sha3": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.8.0.tgz", - "integrity": "sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==" - }, "js-string-escape": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/js-string-escape/-/js-string-escape-1.0.1.tgz", @@ -34696,11 +34662,6 @@ "to-regex": "^3.0.2" } }, - "microseconds": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/microseconds/-/microseconds-0.2.0.tgz", - "integrity": "sha512-n7DHHMjR1avBbSpsTBj6fmMGh2AGrifVV4e+WYc3Q9lO+xnSZ3NyhcBND3vzzatt05LFhoKFRxrIyklmLlUtyA==" - }, "miller-rabin": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/miller-rabin/-/miller-rabin-4.0.1.tgz", @@ -35024,14 +34985,6 @@ "integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==", "optional": true }, - "nano-time": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/nano-time/-/nano-time-1.0.0.tgz", - "integrity": "sha1-sFVPaa2J4i0JB/ehKwmTpdlhN+8=", - "requires": { - "big-integer": "^1.6.16" - } - }, "nanoid": { "version": "3.1.25", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.25.tgz", @@ -35608,11 +35561,6 @@ "integrity": "sha512-eJJDYkhJFFbBBAxeh8xW+weHlkI28n2ZdQV/J/DNfWfSKlGEf2xcfAbZTv3riEXHAhL9SVOTs2pRmXiSTf78xg==", "dev": true }, - "oblivious-set": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/oblivious-set/-/oblivious-set-1.0.0.tgz", - "integrity": "sha512-z+pI07qxo4c2CulUHCDf9lcqDlMSo72N/4rLUpRXf6fu+q8vjt8y0xS+Tlf8NTJDdTXHbdeO1n3MlbctwEoXZw==" - }, "obuf": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/obuf/-/obuf-1.1.2.tgz", @@ -43488,15 +43436,6 @@ "resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz", "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==" }, - "unload": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/unload/-/unload-2.2.0.tgz", - "integrity": "sha512-B60uB5TNBLtN6/LsgAf3udH9saB5p7gqJwcFfbOEZ8BcBHnGwCf6G/TGiEqkRAxX7zAFIUtzdrXQSdL3Q/wqNA==", - "requires": { - "@babel/runtime": "^7.6.2", - "detect-node": "^2.0.4" - } - }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/package.json b/package.json index c8da72348db4..0ec54bb83410 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "@react-navigation/native": "6.0.2", "@react-navigation/stack": "6.0.7", "babel-plugin-transform-remove-console": "^6.9.4", - "broadcast-channel": "^4.2.0", "dotenv": "^8.2.0", "electron-context-menu": "^2.3.0", "electron-log": "^4.3.5", diff --git a/src/ONYXKEYS.js b/src/ONYXKEYS.js index 2290eb69c3f6..ac19970dc635 100755 --- a/src/ONYXKEYS.js +++ b/src/ONYXKEYS.js @@ -131,4 +131,7 @@ export default { // Stores Workspace ID that will be tied to reimbursement account during setup REIMBURSEMENT_ACCOUNT_WORKSPACE_ID: 'reimbursementAccountWorkspaceID', + + // Notifies all tabs that they should sign out and clear storage. + SHOULD_SIGN_OUT: 'shouldSignOut', }; diff --git a/src/components/SignoutManager/index.js b/src/components/SignoutManager/index.js deleted file mode 100644 index 7d0e6cbdd39f..000000000000 --- a/src/components/SignoutManager/index.js +++ /dev/null @@ -1,43 +0,0 @@ - -import {BroadcastChannel} from 'broadcast-channel'; - -const SIGNOUT_EVENT = 'signout'; -const SIGNOUT_CHANNEL = 'signout_channel'; - -const channel = new BroadcastChannel(SIGNOUT_CHANNEL); -let signoutCallback = () => {}; - -channel.addEventListener('message', ({event, errorMessage}) => { - if (event !== SIGNOUT_EVENT) { - return; - } - - signoutCallback(errorMessage); -}); - -/** - * @param {Function} callback - */ -function registerSignoutCallback(callback) { - signoutCallback = callback; -} - -/** - * @param {String} errorMessage - */ -function notifyTabsOfSignout(errorMessage) { - channel.postMessage({event: SIGNOUT_EVENT, errorMessage}); -} - -/** - * @param {String} errorMessage - */ -function signOut(errorMessage) { - signoutCallback(errorMessage) - .then(() => notifyTabsOfSignout(errorMessage)); -} - -export default { - signOut, - registerSignoutCallback, -}; diff --git a/src/components/SignoutManager/index.native.js b/src/components/SignoutManager/index.native.js deleted file mode 100644 index 8a11332308ee..000000000000 --- a/src/components/SignoutManager/index.native.js +++ /dev/null @@ -1,20 +0,0 @@ -let signoutCallback = () => {}; - -/** - * @param {Function} callback - */ -function registerSignoutCallback(callback) { - signoutCallback = callback; -} - -/** - * @param {String} errorMessage - */ -function signOut(errorMessage) { - signoutCallback(errorMessage); -} - -export default { - signOut, - registerSignoutCallback, -}; diff --git a/src/libs/SignoutManager.js b/src/libs/SignoutManager.js new file mode 100644 index 000000000000..065d8249cd10 --- /dev/null +++ b/src/libs/SignoutManager.js @@ -0,0 +1,38 @@ +import Onyx from 'react-native-onyx'; +import ONYXKEYS from '../ONYXKEYS'; + +let signoutCallback = () => {}; +let errorMessage = ''; +let shouldSignOut = false; +Onyx.connect({ + key: ONYXKEYS.SHOULD_SIGN_OUT, + callback: (val) => { + if (!shouldSignOut && val) { + signoutCallback(errorMessage); + errorMessage = ''; + Onyx.set(ONYXKEYS.SHOULD_SIGN_OUT, false); + } + + shouldSignOut = val; + }, +}); + +/** + * @param {Function} callback + */ +function registerSignoutCallback(callback) { + signoutCallback = callback; +} + +/** + * @param {String} message + */ +function signOut(message) { + errorMessage = message; + Onyx.set(ONYXKEYS.SHOULD_SIGN_OUT, true); +} + +export default { + signOut, + registerSignoutCallback, +}; diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 3b2f36558a16..a3fdb9607027 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -1,5 +1,5 @@ import Onyx from 'react-native-onyx'; -import SignoutManager from '../../components/SignoutManager'; +import SignoutManager from '../SignoutManager'; import ONYXKEYS from '../../ONYXKEYS'; let currentActiveClients; From ccef3d903dc3affe7ddd85aa5d3166930f714899 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 28 Sep 2021 06:22:58 -1000 Subject: [PATCH 8/8] remove return promise --- src/libs/actions/SignInRedirect.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index a3fdb9607027..95e8b862d8bc 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -18,27 +18,23 @@ Onyx.connect({ /** * @param {String} errorMessage - * @returns {Promise} */ function clearStorageAndRedirect(errorMessage) { const activeClients = currentActiveClients; const preferredLocale = currentPreferredLocale; // Clearing storage discards the authToken. This causes a redirect to the SignIn screen - return Onyx.clear() + Onyx.clear() .then(() => { - const promises = []; - if (preferredLocale) { - promises.push(Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale)); + Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); } if (activeClients && activeClients.length > 0) { - promises.push(Onyx.set(ONYXKEYS.ACTIVE_CLIENTS, activeClients)); + Onyx.set(ONYXKEYS.ACTIVE_CLIENTS, activeClients); } // `Onyx.clear` reinitialize the Onyx instance with initial values so use `Onyx.merge` instead of `Onyx.set`. - promises.push(Onyx.merge(ONYXKEYS.SESSION, {error: errorMessage})); - return Promise.all(promises); + Onyx.merge(ONYXKEYS.SESSION, {error: errorMessage}); }); }