-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #415 from amam-deriv/amam/update-logout-missing-af…
…filiate-utm amam/P2PS-4572/remove cookies upon logging out
- Loading branch information
Showing
5 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}); | ||
}; |