diff --git a/lib/shared/data-utils.js b/lib/shared/data-utils.js new file mode 100644 --- /dev/null +++ b/lib/shared/data-utils.js @@ -0,0 +1,7 @@ +// @flow + +function shouldClearData(oldUserID: ?string, newUserID: ?string): boolean { + return !!oldUserID && oldUserID !== newUserID; +} + +export { shouldClearData }; diff --git a/native/data/sqlite-data-handler.js b/native/data/sqlite-data-handler.js --- a/native/data/sqlite-data-handler.js +++ b/native/data/sqlite-data-handler.js @@ -16,6 +16,7 @@ import { userStoreOpsHandlers } from 'lib/ops/user-store-ops.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { useInitialNotificationsEncryptedMessage } from 'lib/shared/crypto-utils.js'; +import { shouldClearData } from 'lib/shared/data-utils.js'; import { recoveryActionSources, type RecoveryActionSource, @@ -138,10 +139,7 @@ const handleSensitiveData = React.useCallback(async () => { try { const databaseCurrentUserInfoID = await commCoreModule.getCurrentUserID(); - if ( - databaseCurrentUserInfoID && - databaseCurrentUserInfoID !== currentLoggedInUserID - ) { + if (shouldClearData(databaseCurrentUserInfoID, currentLoggedInUserID)) { await callClearSensitiveData('change in logged-in user credentials'); } if (currentLoggedInUserID) { diff --git a/native/redux/redux-setup.js b/native/redux/redux-setup.js --- a/native/redux/redux-setup.js +++ b/native/redux/redux-setup.js @@ -21,6 +21,7 @@ import { reduceLoadingStatuses } from 'lib/reducers/loading-reducer.js'; import baseReducer from 'lib/reducers/master-reducer.js'; import { reduceCurrentUserInfo } from 'lib/reducers/user-reducer.js'; +import { shouldClearData } from 'lib/shared/data-utils.js'; import { invalidSessionDowngrade, invalidSessionRecovery, @@ -270,7 +271,7 @@ ); if ( !state.currentUserInfo?.anonymous && - newCurrentUserInfo?.id !== state.currentUserInfo?.id + shouldClearData(state.currentUserInfo?.id, newCurrentUserInfo?.id) ) { state = resetUserSpecificState( state, diff --git a/web/redux/redux-setup.js b/web/redux/redux-setup.js --- a/web/redux/redux-setup.js +++ b/web/redux/redux-setup.js @@ -24,6 +24,7 @@ import { reduceCurrentUserInfo } from 'lib/reducers/user-reducer.js'; import { mostRecentlyReadThreadSelector } from 'lib/selectors/thread-selectors.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; +import { shouldClearData } from 'lib/shared/data-utils.js'; import { invalidSessionDowngrade, identityInvalidSessionDowngrade, @@ -341,7 +342,7 @@ ); if ( !state.currentUserInfo?.anonymous && - newCurrentUserInfo?.id !== state.currentUserInfo?.id + shouldClearData(state.currentUserInfo?.id, newCurrentUserInfo?.id) ) { state = resetUserSpecificState( state, diff --git a/web/shared-worker/sqlite-data-handler.js b/web/shared-worker/sqlite-data-handler.js --- a/web/shared-worker/sqlite-data-handler.js +++ b/web/shared-worker/sqlite-data-handler.js @@ -2,6 +2,7 @@ import * as React from 'react'; +import { shouldClearData } from 'lib/shared/data-utils.js'; import { getMessageForException } from 'lib/utils/errors.js'; import { useDispatch } from 'lib/utils/redux-utils.js'; @@ -36,11 +37,10 @@ ); } - if (currentDBUserID === currentLoggedInUserID && !errorGettingUserID) { - return; - } - - if (currentDBUserID || errorGettingUserID) { + if ( + errorGettingUserID || + shouldClearData(currentDBUserID, currentLoggedInUserID) + ) { try { await sharedWorker.init({ clearDatabase: true }); } catch (error) { @@ -53,7 +53,8 @@ return; } } - if (currentLoggedInUserID) { + + if (currentLoggedInUserID && currentLoggedInUserID !== currentDBUserID) { try { await sharedWorker.schedule({ type: workerRequestMessageTypes.SET_CURRENT_USER_ID,