From 009fb3785cd39db451c65a2e779556b339e7818d Mon Sep 17 00:00:00 2001 From: ameerul-deriv Date: Wed, 18 Dec 2024 16:18:37 +0800 Subject: [PATCH] chore: handle authtoken sync for logged in and logged out users --- public/localstorage-sync.html | 2 +- src/hooks/custom-hooks/useOAuth.ts | 50 ++++++++++++++---------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/public/localstorage-sync.html b/public/localstorage-sync.html index c06eee15..381cccd8 100644 --- a/public/localstorage-sync.html +++ b/public/localstorage-sync.html @@ -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; } diff --git a/src/hooks/custom-hooks/useOAuth.ts b/src/hooks/custom-hooks/useOAuth.ts index 2701e48d..08f4fe1e 100644 --- a/src/hooks/custom-hooks/useOAuth.ts +++ b/src/hooks/custom-hooks/useOAuth.ts @@ -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; @@ -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 }; };