diff --git a/src/libs/CurrentUserStore.ts b/src/libs/CurrentUserStore.ts index fd5588f03b1a..d44cb3410fc3 100644 --- a/src/libs/CurrentUserStore.ts +++ b/src/libs/CurrentUserStore.ts @@ -1,19 +1,20 @@ import ONYXKEYS from '@src/ONYXKEYS'; /** - * Thin store for current user email that has no dependencies on Log. - * This avoids circular dependency: Log -> NetworkStore -> Log - * Other modules can import getCurrentUserEmail from NetworkStore for convenience, - * but Log specifically imports from here to break the cycle. + * Session email/auth-token mirror with no imports beyond Onyx. Keeps Log and light + * consumers away from NetworkStore, which imports Log, and from + * actions/Session, which drags the whole session layer into their import graphs. */ import Onyx from 'react-native-onyx'; let currentUserEmail: string | null = null; +let sessionAuthToken: string | null = null; Onyx.connectWithoutView({ key: ONYXKEYS.SESSION, callback: (val) => { currentUserEmail = val?.email ?? null; + sessionAuthToken = val?.authToken ?? null; }, }); @@ -21,5 +22,8 @@ function getCurrentUserEmail(): string | null { return currentUserEmail; } -// eslint-disable-next-line import/prefer-default-export -export {getCurrentUserEmail}; +function hasAuthToken(): boolean { + return !!sessionAuthToken; +} + +export {getCurrentUserEmail, hasAuthToken}; diff --git a/src/libs/Navigation/linkingConfig/subscribe.ts b/src/libs/Navigation/linkingConfig/subscribe.ts index fbad0b6a60e2..c746d9eac4c9 100644 --- a/src/libs/Navigation/linkingConfig/subscribe.ts +++ b/src/libs/Navigation/linkingConfig/subscribe.ts @@ -1,5 +1,5 @@ -import {hasAuthToken} from '@libs/actions/Session'; import continuePlaidOAuth from '@libs/continuePlaidOAuth'; +import {hasAuthToken} from '@libs/CurrentUserStore'; import navigationRef from '@libs/Navigation/navigationRef'; import type {RootNavigatorParamList} from '@libs/Navigation/types'; diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 20a8ea435683..33fb046a2453 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -21,6 +21,7 @@ import type { import type SignInUserParams from '@libs/API/parameters/SignInUserParams'; import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; import asyncOpenURL from '@libs/asyncOpenURL'; +import {hasAuthToken} from '@libs/CurrentUserStore'; import * as ErrorUtils from '@libs/ErrorUtils'; import FraudProtection from '@libs/FraudProtection'; import getPlatform from '@libs/getPlatform'; @@ -333,13 +334,6 @@ function hasStashedSession(stashedSessionParam: Session | undefined, stashedCred return !!(stashedSessionParam?.authToken && stashedCredentialsParam?.autoGeneratedLogin && stashedCredentialsParam.autoGeneratedLogin !== ''); } -/** - * Checks if the user has authToken - */ -function hasAuthToken(): boolean { - return !!deprecatedSession.authToken; -} - /** * Indicates if the session which creation date is in parameter is expired * @param sessionCreationDate the session creation date timestamp diff --git a/tests/unit/CurrentUserStoreTest.ts b/tests/unit/CurrentUserStoreTest.ts new file mode 100644 index 000000000000..b6ad7dfbe59c --- /dev/null +++ b/tests/unit/CurrentUserStoreTest.ts @@ -0,0 +1,30 @@ +import {hasAuthToken} from '@libs/CurrentUserStore'; + +import ONYXKEYS from '@src/ONYXKEYS'; + +import Onyx from 'react-native-onyx'; + +describe('hasAuthToken', () => { + beforeAll(() => { + Onyx.init({keys: ONYXKEYS}); + }); + + beforeEach(() => Onyx.clear()); + + it('returns false while the session holds no auth token', () => + Onyx.merge(ONYXKEYS.SESSION, {email: 'user@test.com'}).then(() => { + expect(hasAuthToken()).toBe(false); + })); + + it('returns true once the session holds an auth token', () => + Onyx.merge(ONYXKEYS.SESSION, {authToken: 'abc123'}).then(() => { + expect(hasAuthToken()).toBe(true); + })); + + it('returns false again once the auth token is cleared', () => + Onyx.merge(ONYXKEYS.SESSION, {authToken: 'abc123'}) + .then(() => Onyx.merge(ONYXKEYS.SESSION, {authToken: null})) + .then(() => { + expect(hasAuthToken()).toBe(false); + })); +}); diff --git a/tests/unit/Navigation/linkingConfigSubscribeTest.ts b/tests/unit/Navigation/linkingConfigSubscribeTest.ts index d8bc2a91132d..3fb67b066245 100644 --- a/tests/unit/Navigation/linkingConfigSubscribeTest.ts +++ b/tests/unit/Navigation/linkingConfigSubscribeTest.ts @@ -1,9 +1,11 @@ -import {hasAuthToken} from '@libs/actions/Session'; +import type * as CurrentUserStore from '@libs/CurrentUserStore'; +import {hasAuthToken} from '@libs/CurrentUserStore'; import subscribe from '@libs/Navigation/linkingConfig/subscribe'; import {Linking} from 'react-native'; -jest.mock('@libs/actions/Session', () => ({ +jest.mock('@libs/CurrentUserStore', () => ({ + ...jest.requireActual('@libs/CurrentUserStore'), hasAuthToken: jest.fn(), }));