Skip to content

Commit

Permalink
chore: handle authtoken sync for logged in and logged out users
Browse files Browse the repository at this point in the history
  • Loading branch information
ameerul-deriv committed Dec 18, 2024
1 parent 3253529 commit 009fb37
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
2 changes: 1 addition & 1 deletion public/localstorage-sync.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

localStorage.setItem(message.data.key, JSON.stringify(accounts));

// localStorage.setItem('authToken', accounts[active_loginid]);
localStorage.setItem('authToken', accounts[active_loginid]);
}
break;
}
Expand Down
50 changes: 24 additions & 26 deletions src/hooks/custom-hooks/useOAuth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useCallback } from 'react';
import Cookies from 'js-cookie';
// import { BUY_SELL_URL, getOauthUrl } from '@/constants';
import { getOauthUrl } from '@/constants';
import { getCurrentRoute, removeCookies } from '@/utils';
import { useAuthData } from '@deriv-com/api-hooks';
import { TOAuth2EnabledAppList, useOAuth2 } from '@deriv-com/auth-client';
import useGrowthbookGetFeatureValue from './useGrowthbookGetFeatureValue';
import useOAuth2Enabled from './useOAuth2Enabled';

type UseOAuthReturn = {
oAuthLogout: () => void;
Expand All @@ -26,49 +25,48 @@ const useOAuth = (): UseOAuthReturn => {
OAuth2EnabledAppsInitialised,
};

const [isOAuth2Enabled] = useOAuth2Enabled();

const { logout } = useAuthData();
const { error, isAuthorized, isAuthorizing } = useAuthData();
const isEndpointPage = getCurrentRoute() === 'endpoint';
const isRedirectPage = getCurrentRoute() === 'redirect';
// const oauthUrl = getOauthUrl();
const oauthUrl = getOauthUrl();
const authTokenLocalStorage = localStorage.getItem('authToken');

const WSLogoutAndRedirect = async () => {
await logout();
removeCookies('affiliate_token', 'affiliate_tracking', 'utm_data', 'onfido_token', 'gclid');
// window.open(oauthUrl, '_self');
window.open(oauthUrl, '_self');
};
const { OAuth2Logout: oAuthLogout } = useOAuth2(oAuthGrowthbookConfig, WSLogoutAndRedirect);

// console.log('isOAuth2Enabled', isOAuth2Enabled);

const onRenderAuthCheck = useCallback(() => {
if (!isEndpointPage) {
if (error?.code === 'InvalidToken') {
oAuthLogout();
} else if (!isAuthorized && !isAuthorizing) {
if (isRedirectPage && !isOAuth2Enabled) {
const params = new URLSearchParams(location.search);
const from = params.get('from');
if (from === 'tradershub') {
const authTokenCookie = Cookies.get('authtoken');
} else if (isRedirectPage) {
const params = new URLSearchParams(location.search);
const from = params.get('from');
const authTokenCookie = Cookies.get('authtoken');

if (authTokenCookie) {
localStorage.setItem('authToken', authTokenCookie);
localStorage.setItem('authTokentest', authTokenCookie);
params.delete('from');
window.location.href = window.location.origin;
} else {
// console.log('auth token cookie not found');
}
}
} else {
// window.open(oauthUrl, '_self');
if (from === 'tradershub' && authTokenCookie) {
localStorage.setItem('authToken', authTokenCookie);
Cookies.remove('authtoken');
window.location.href = window.location.origin;
}
} else if (!isAuthorized && !isAuthorizing && !authTokenLocalStorage) {
window.open(oauthUrl, '_self');
}
}
}, [isEndpointPage, error?.code, isAuthorized, isAuthorizing, oAuthLogout, isRedirectPage, isOAuth2Enabled]);
}, [
isEndpointPage,
error?.code,
isRedirectPage,
isAuthorized,
isAuthorizing,
authTokenLocalStorage,
oAuthLogout,
oauthUrl,
]);

return { oAuthLogout, onRenderAuthCheck };
};
Expand Down

0 comments on commit 009fb37

Please sign in to comment.