Skip to content

Commit

Permalink
Merge pull request #415 from amam-deriv/amam/update-logout-missing-af…
Browse files Browse the repository at this point in the history
…filiate-utm

amam/P2PS-4572/remove cookies upon logging out
  • Loading branch information
amam-deriv authored Dec 4, 2024
2 parents 3bb19b4 + 336b742 commit 2171aaf
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/components/AppHeader/__tests__/AppHeader.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ describe('<AppHeader/>', () => {
writable: true,
});

Object.defineProperty(document, 'domain', {
value: 'example.com',
writable: true,
});

render(
<BrowserRouter>
<QueryParamProvider adapter={ReactRouter5Adapter}>
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/custom-hooks/useOAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
import { getOauthUrl } from '@/constants';
import { getCurrentRoute } from '@/utils';
import { getCurrentRoute, removeCookies } from '@/utils';
import { useAuthData } from '@deriv-com/api-hooks';
import { TOAuth2EnabledAppList, useOAuth2 } from '@deriv-com/auth-client';
import useGrowthbookGetFeatureValue from './useGrowthbookGetFeatureValue';
Expand Down Expand Up @@ -31,6 +31,7 @@ const useOAuth = (): UseOAuthReturn => {

const WSLogoutAndRedirect = async () => {
await logout();
removeCookies('affiliate_token', 'affiliate_tracking', 'utm_data', 'onfido_token', 'gclid');
window.open(oauthUrl, '_self');
};
const { OAuth2Logout: oAuthLogout } = useOAuth2(oAuthGrowthbookConfig, WSLogoutAndRedirect);
Expand Down
37 changes: 37 additions & 0 deletions src/utils/__tests__/storage.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Cookies from 'js-cookie';
import { removeCookies } from '../storage';

jest.mock('js-cookie');

describe('removeCookies', () => {
beforeEach(() => {
jest.clearAllMocks();
Object.defineProperty(window, 'location', {
value: { pathname: '/test' },
writable: true,
});
Object.defineProperty(document, 'domain', {
value: 'example.com',
writable: true,
});
});

it('should remove cookies from all specified domains and paths', () => {
removeCookies('cookie1', 'cookie2');

expect(Cookies.remove).toHaveBeenCalledWith('cookie1', { domain: '.example.com', path: '/' });
expect(Cookies.remove).toHaveBeenCalledWith('cookie1', { domain: '.example.com', path: '/' });
expect(Cookies.remove).toHaveBeenCalledWith('cookie1');
expect(Cookies.remove).toHaveBeenCalledWith('cookie2', { domain: '.example.com', path: '/' });
expect(Cookies.remove).toHaveBeenCalledWith('cookie2', { domain: '.example.com', path: '/' });
expect(Cookies.remove).toHaveBeenCalledWith('cookie2');
});

it('should not remove cookies from parent path if it does not exist', () => {
window.location.pathname = '/';
removeCookies('cookie1', 'cookie2');

expect(Cookies.remove).not.toHaveBeenCalledWith('cookie1', { domain: '.example.com', path: '/test' });
expect(Cookies.remove).not.toHaveBeenCalledWith('cookie2', { domain: '.example.com', path: '/test' });
});
});
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './os';
export * from './payment-methods';
export * from './routes';
export * from './setup-mocks';
export * from './storage';
export * from './string';
export * from './time';
export * from './types';
22 changes: 22 additions & 0 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Cookies from 'js-cookie';

export const removeCookies = (...cookieNames: string[]) => {
const currentDomain = document.domain ?? '';
const domains = [`.${currentDomain.split('.').slice(-2).join('.')}`, `.${currentDomain}`];

let parentPath = window.location.pathname.split('/', 2)[1];
if (parentPath !== '') {
parentPath = `/${parentPath}`;
}

cookieNames.forEach(c => {
Cookies.remove(c, { domain: domains[0], path: '/' });
Cookies.remove(c, { domain: domains[1], path: '/' });
Cookies.remove(c);
if (new RegExp(c).test(document.cookie) && parentPath) {
Cookies.remove(c, { domain: domains[0], path: parentPath });
Cookies.remove(c, { domain: domains[1], path: parentPath });
Cookies.remove(c, { path: parentPath });
}
});
};

0 comments on commit 2171aaf

Please sign in to comment.