Skip to content

Commit

Permalink
Merge pull request #97 from thisyahlen-deriv/thisyahlen/cookie-domain…
Browse files Browse the repository at this point in the history
…-set

chore: add cookie domain setter
  • Loading branch information
thisyahlen-deriv authored Nov 22, 2024
2 parents 7e680d9 + bc2450c commit b5144cf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/components/Callback/Callback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,16 @@ export const Callback = ({
const legacyTokens = await requestLegacyToken(accessToken);

onSignInSuccess?.(legacyTokens);
Cookies.set('logged_state', 'true', {
expires: 30,
path: '/',
});
const domains = ['deriv.com', 'binary.sx', 'pages.dev', 'localhost'];
const currentDomain = window.location.hostname.split('.').slice(-2).join('.');
if (domains.includes(currentDomain)) {
Cookies.set('logged_state', 'true', {
expires: 30,
path: '/',
domain: currentDomain,
secure: true,
});
}
}
} catch (err) {
if (err instanceof Error) {
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/__tests__/useOAuth2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ describe('useOAuth2', () => {
window.dispatchEvent(event);

expect(Cookies.set).toHaveBeenCalledWith('logged_state', 'false', {
domain: 'localhost',
secure: true,
expires: 30,
path: '/',
});
Expand Down
14 changes: 10 additions & 4 deletions src/hooks/useOAuth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ export const useOAuth2 = (OAuth2GrowthBookConfig: OAuth2GBConfig, WSLogoutAndRed

const onMessage = (event: MessageEvent) => {
if (event.data === 'logout_complete') {
Cookies.set('logged_state', 'false', {
expires: 30,
path: '/',
});
const domains = ['deriv.com', 'binary.sx', 'pages.dev', 'localhost'];
const currentDomain = window.location.hostname.split('.').slice(-2).join('.');
if (domains.includes(currentDomain)) {
Cookies.set('logged_state', 'false', {
expires: 30,
path: '/',
domain: currentDomain,
secure: true,
});
}
WSLogoutAndRedirect();
window.removeEventListener('message', onMessage);
cleanup();
Expand Down

0 comments on commit b5144cf

Please sign in to comment.