From 6d4f537bbdb16774de6ae8025905fd8a2a193c92 Mon Sep 17 00:00:00 2001 From: Alex Beaman Date: Tue, 28 Jun 2022 20:29:18 +0200 Subject: [PATCH 01/10] Use new API endpoint LogOut --- src/libs/actions/Session/index.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/libs/actions/Session/index.js b/src/libs/actions/Session/index.js index 1cde31b65674..a17e425fb680 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -21,6 +21,7 @@ import * as ValidationUtils from '../../ValidationUtils'; import * as Authentication from '../../Authentication'; import * as ErrorUtils from '../../ErrorUtils'; import * as Welcome from '../Welcome'; +import * as API from '../../API'; let credentials = {}; Onyx.connect({ @@ -72,25 +73,32 @@ function createAccount(login) { */ function signOut() { Log.info('Flushing logs before signing out', true, {}, true); + Timing.clearData(); + if (credentials && credentials.autoGeneratedLogin) { - // Clean up the login that we created - DeprecatedAPI.DeleteLogin({ + const optimisticData = [ + { + onyxMethod: 'set', + key: ONYXKEYS.SESSION, + value: null, + }, + { + onyxMethod: 'set', + key: ONYXKEYS.CREDENTIALS, + value: null, + } + ]; + API.write('LogOut', { partnerUserID: credentials.autoGeneratedLogin, partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, shouldRetry: false, - }) - .then((response) => { - if (response.jsonCode === CONST.JSON_CODE.SUCCESS) { - return; - } - - Onyx.merge(ONYXKEYS.SESSION, {error: response.message}); - }); + }, {optimisticData}); + return; } + Onyx.set(ONYXKEYS.SESSION, null); Onyx.set(ONYXKEYS.CREDENTIALS, null); - Timing.clearData(); } function signOutAndRedirectToSignIn() { From d3af1643928826310184b64204ebea260280666f Mon Sep 17 00:00:00 2001 From: Alex Beaman Date: Tue, 28 Jun 2022 20:54:13 +0200 Subject: [PATCH 02/10] Add missing comma --- 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 a17e425fb680..094fb7417868 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -86,7 +86,7 @@ function signOut() { onyxMethod: 'set', key: ONYXKEYS.CREDENTIALS, value: null, - } + }, ]; API.write('LogOut', { partnerUserID: credentials.autoGeneratedLogin, From 06fb754bb679509b2fccc45406dda10e4f3fdf81 Mon Sep 17 00:00:00 2001 From: Alex Beaman Date: Wed, 29 Jun 2022 13:26:05 +0200 Subject: [PATCH 03/10] Remove whitespace --- 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 094fb7417868..988bd61abd42 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -87,7 +87,7 @@ function signOut() { key: ONYXKEYS.CREDENTIALS, value: null, }, - ]; + ]; API.write('LogOut', { partnerUserID: credentials.autoGeneratedLogin, partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, From c32335f901b6e97b5bfc364232e97a5fbb24d8b3 Mon Sep 17 00:00:00 2001 From: Alex Beaman Date: Wed, 29 Jun 2022 13:26:20 +0200 Subject: [PATCH 04/10] Prevent auth attempt if no creds stored --- src/libs/Authentication.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libs/Authentication.js b/src/libs/Authentication.js index cd32c958eb25..dc982eda9886 100644 --- a/src/libs/Authentication.js +++ b/src/libs/Authentication.js @@ -58,10 +58,19 @@ function Authenticate(parameters) { * @returns {Promise} */ function reauthenticate(command = '') { + const credentials = NetworkStore.getCredentials(); + + // Hack to prevent Authenticate.reauthenticate from being called after calling new LogOut command + // LogOut used to have `shouldRetry: false` which prevented this in the old middleware, but now + // shouldRetry is hard coded to `true` in `API.write` + if (!credentials) { + NetworkStore.setIsAuthenticating(false); + return; + } + // Prevent any more requests from being processed while authentication happens NetworkStore.setIsAuthenticating(true); - const credentials = NetworkStore.getCredentials(); return Authenticate({ useExpensifyLogin: false, partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, From c7fdd605b402a3b64db52e82bb3c9ef218dd48af Mon Sep 17 00:00:00 2001 From: Alex Beaman Date: Wed, 29 Jun 2022 14:54:57 +0200 Subject: [PATCH 05/10] Reset authenticate code --- src/libs/Authentication.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/libs/Authentication.js b/src/libs/Authentication.js index dc982eda9886..cd32c958eb25 100644 --- a/src/libs/Authentication.js +++ b/src/libs/Authentication.js @@ -58,19 +58,10 @@ function Authenticate(parameters) { * @returns {Promise} */ function reauthenticate(command = '') { - const credentials = NetworkStore.getCredentials(); - - // Hack to prevent Authenticate.reauthenticate from being called after calling new LogOut command - // LogOut used to have `shouldRetry: false` which prevented this in the old middleware, but now - // shouldRetry is hard coded to `true` in `API.write` - if (!credentials) { - NetworkStore.setIsAuthenticating(false); - return; - } - // Prevent any more requests from being processed while authentication happens NetworkStore.setIsAuthenticating(true); + const credentials = NetworkStore.getCredentials(); return Authenticate({ useExpensifyLogin: false, partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, From c71ee2441f63787eb4918638b2fdee375ac632d1 Mon Sep 17 00:00:00 2001 From: Alex Beaman Date: Wed, 29 Jun 2022 14:55:24 +0200 Subject: [PATCH 06/10] Send authToken with command --- src/libs/actions/Session/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libs/actions/Session/index.js b/src/libs/actions/Session/index.js index 988bd61abd42..bc9b85dc0b76 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -22,6 +22,7 @@ import * as Authentication from '../../Authentication'; import * as ErrorUtils from '../../ErrorUtils'; import * as Welcome from '../Welcome'; import * as API from '../../API'; +import * as NetworkStore from '../../Network/NetworkStore'; let credentials = {}; Onyx.connect({ @@ -89,6 +90,8 @@ function signOut() { }, ]; API.write('LogOut', { + // Send current authToken because we will immediately clear it once triggering this command + authToken: NetworkStore.getAuthToken(), partnerUserID: credentials.autoGeneratedLogin, partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, From 45ecc5d88f33cf4f1f80d41129c2b688d2ee8624 Mon Sep 17 00:00:00 2001 From: Alex Beaman Date: Fri, 1 Jul 2022 18:20:50 +0200 Subject: [PATCH 07/10] Make sure timing still works --- src/libs/actions/Session/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Session/index.js b/src/libs/actions/Session/index.js index bc9b85dc0b76..85054fd8cc63 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -74,7 +74,6 @@ function createAccount(login) { */ function signOut() { Log.info('Flushing logs before signing out', true, {}, true); - Timing.clearData(); if (credentials && credentials.autoGeneratedLogin) { const optimisticData = [ @@ -97,11 +96,12 @@ function signOut() { partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, shouldRetry: false, }, {optimisticData}); - return; + } else { + Onyx.set(ONYXKEYS.SESSION, null); + Onyx.set(ONYXKEYS.CREDENTIALS, null); } - Onyx.set(ONYXKEYS.SESSION, null); - Onyx.set(ONYXKEYS.CREDENTIALS, null); + Timing.clearData(); } function signOutAndRedirectToSignIn() { From 7630b25de78a66753dceaf01f21fa6f349c8f76b Mon Sep 17 00:00:00 2001 From: Alex Beaman Date: Tue, 5 Jul 2022 22:06:53 +0200 Subject: [PATCH 08/10] Always call LogOut, consolidate onyx sets --- src/libs/actions/Session/index.js | 45 ++++++++++++++----------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/src/libs/actions/Session/index.js b/src/libs/actions/Session/index.js index 85054fd8cc63..159091ea1d72 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -75,31 +75,26 @@ function createAccount(login) { function signOut() { Log.info('Flushing logs before signing out', true, {}, true); - if (credentials && credentials.autoGeneratedLogin) { - const optimisticData = [ - { - onyxMethod: 'set', - key: ONYXKEYS.SESSION, - value: null, - }, - { - onyxMethod: 'set', - key: ONYXKEYS.CREDENTIALS, - value: null, - }, - ]; - API.write('LogOut', { - // Send current authToken because we will immediately clear it once triggering this command - authToken: NetworkStore.getAuthToken(), - partnerUserID: credentials.autoGeneratedLogin, - partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, - partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, - shouldRetry: false, - }, {optimisticData}); - } else { - Onyx.set(ONYXKEYS.SESSION, null); - Onyx.set(ONYXKEYS.CREDENTIALS, null); - } + const optimisticData = [ + { + onyxMethod: 'set', + key: ONYXKEYS.SESSION, + value: null, + }, + { + onyxMethod: 'set', + key: ONYXKEYS.CREDENTIALS, + value: null, + }, + ]; + API.write('LogOut', { + // Send current authToken because we will immediately clear it once triggering this command + authToken: NetworkStore.getAuthToken(), + partnerUserID: (credentials && credentials.autoGeneratedLogin) || '', + partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, + partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, + shouldRetry: false, + }, {optimisticData}); Timing.clearData(); } From a5207b1fb25be61e523e1a417bb401dee9ca9132 Mon Sep 17 00:00:00 2001 From: Alex Beaman Date: Tue, 5 Jul 2022 22:10:37 +0200 Subject: [PATCH 09/10] Merge branch 'main' of github.com:Expensify/App into beaman-refactorApiDeleteLogin From 9f77bfa03fd33079b24053bc00ca47c4f89f7b26 Mon Sep 17 00:00:00 2001 From: Alex Beaman Date: Wed, 6 Jul 2022 11:09:15 +0200 Subject: [PATCH 10/10] Clean up empty var check --- src/libs/actions/Session/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Session/index.js b/src/libs/actions/Session/index.js index 159091ea1d72..23e8769ff635 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -1,6 +1,7 @@ import Onyx from 'react-native-onyx'; import Str from 'expensify-common/lib/str'; import _ from 'underscore'; +import lodashGet from 'lodash/get'; import ONYXKEYS from '../../../ONYXKEYS'; import redirectToSignIn from '../SignInRedirect'; import * as DeprecatedAPI from '../../deprecatedAPI'; @@ -90,7 +91,7 @@ function signOut() { API.write('LogOut', { // Send current authToken because we will immediately clear it once triggering this command authToken: NetworkStore.getAuthToken(), - partnerUserID: (credentials && credentials.autoGeneratedLogin) || '', + partnerUserID: lodashGet(credentials, 'autoGeneratedLogin', ''), partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, shouldRetry: false,