Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amam/P2PS-4572/remove cookies upon logging out #415

Merged
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
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 });
}
});
};
Loading