Skip to content

Commit 1fd7abe

Browse files
committed
fix: remove cookies upon logging out
1 parent 3bb19b4 commit 1fd7abe

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

src/hooks/custom-hooks/useOAuth.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useCallback } from 'react';
22
import { getOauthUrl } from '@/constants';
3-
import { getCurrentRoute } from '@/utils';
3+
import { getCurrentRoute, removeCookies } from '@/utils';
44
import { useAuthData } from '@deriv-com/api-hooks';
55
import { TOAuth2EnabledAppList, useOAuth2 } from '@deriv-com/auth-client';
66
import useGrowthbookGetFeatureValue from './useGrowthbookGetFeatureValue';
@@ -31,6 +31,7 @@ const useOAuth = (): UseOAuthReturn => {
3131

3232
const WSLogoutAndRedirect = async () => {
3333
await logout();
34+
removeCookies('affiliate_token', 'affiliate_tracking', 'utm_data', 'onfido_token');
3435
window.open(oauthUrl, '_self');
3536
};
3637
const { OAuth2Logout: oAuthLogout } = useOAuth2(oAuthGrowthbookConfig, WSLogoutAndRedirect);

src/utils/__tests__/storage.spec.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Cookies from 'js-cookie';
2+
import { removeCookies } from '../storage';
3+
4+
jest.mock('js-cookie');
5+
6+
describe('removeCookies', () => {
7+
beforeEach(() => {
8+
jest.clearAllMocks();
9+
Object.defineProperty(window, 'location', {
10+
value: { pathname: '/test' },
11+
writable: true,
12+
});
13+
Object.defineProperty(document, 'domain', {
14+
value: 'example.com',
15+
writable: true,
16+
});
17+
});
18+
19+
it('should remove cookies from all specified domains and paths', () => {
20+
removeCookies('cookie1', 'cookie2');
21+
22+
expect(Cookies.remove).toHaveBeenCalledWith('cookie1', { domain: '.example.com', path: '/' });
23+
expect(Cookies.remove).toHaveBeenCalledWith('cookie1', { domain: '.example.com', path: '/' });
24+
expect(Cookies.remove).toHaveBeenCalledWith('cookie1');
25+
expect(Cookies.remove).toHaveBeenCalledWith('cookie2', { domain: '.example.com', path: '/' });
26+
expect(Cookies.remove).toHaveBeenCalledWith('cookie2', { domain: '.example.com', path: '/' });
27+
expect(Cookies.remove).toHaveBeenCalledWith('cookie2');
28+
});
29+
30+
it('should not remove cookies from parent path if it does not exist', () => {
31+
window.location.pathname = '/';
32+
removeCookies('cookie1', 'cookie2');
33+
34+
expect(Cookies.remove).not.toHaveBeenCalledWith('cookie1', { domain: '.example.com', path: '/test' });
35+
expect(Cookies.remove).not.toHaveBeenCalledWith('cookie2', { domain: '.example.com', path: '/test' });
36+
});
37+
});

src/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export * from './os';
1313
export * from './payment-methods';
1414
export * from './routes';
1515
export * from './setup-mocks';
16+
export * from './storage';
1617
export * from './string';
1718
export * from './time';
1819
export * from './types';

src/utils/storage.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Cookies from 'js-cookie';
2+
3+
export const removeCookies = (...cookieNames: string[]) => {
4+
const domains = [`.${document.domain.split('.').slice(-2).join('.')}`, `.${document.domain}`];
5+
6+
let parentPath = window.location.pathname.split('/', 2)[1];
7+
if (parentPath !== '') {
8+
parentPath = `/${parentPath}`;
9+
}
10+
11+
cookieNames.forEach(c => {
12+
Cookies.remove(c, { domain: domains[0], path: '/' });
13+
Cookies.remove(c, { domain: domains[1], path: '/' });
14+
Cookies.remove(c);
15+
if (new RegExp(c).test(document.cookie) && parentPath) {
16+
Cookies.remove(c, { domain: domains[0], path: parentPath });
17+
Cookies.remove(c, { domain: domains[1], path: parentPath });
18+
Cookies.remove(c, { path: parentPath });
19+
}
20+
});
21+
};

0 commit comments

Comments
 (0)