Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/libs/CurrentUserStore.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
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;
},
});

function getCurrentUserEmail(): string | null {
return currentUserEmail;
}

// eslint-disable-next-line import/prefer-default-export
export {getCurrentUserEmail};
function hasAuthToken(): boolean {
return !!sessionAuthToken;
}

export {getCurrentUserEmail, hasAuthToken};
2 changes: 1 addition & 1 deletion src/libs/Navigation/linkingConfig/subscribe.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
8 changes: 1 addition & 7 deletions src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/CurrentUserStoreTest.ts
Original file line number Diff line number Diff line change
@@ -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);
}));
});
6 changes: 4 additions & 2 deletions tests/unit/Navigation/linkingConfigSubscribeTest.ts
Original file line number Diff line number Diff line change
@@ -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<typeof CurrentUserStore>('@libs/CurrentUserStore'),
hasAuthToken: jest.fn(),
}));

Expand Down
Loading