From 769cadedd5e03536d4ffdfd148db78f90ee262d7 Mon Sep 17 00:00:00 2001 From: Adrienne Rio Date: Mon, 9 Dec 2024 16:58:51 +0800 Subject: [PATCH] chore: used oauth2logout as logout function --- src/javascript/_common/auth.js | 42 +++++++++++++++++++++++++++++++-- src/javascript/app/base/page.js | 5 +++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/javascript/_common/auth.js b/src/javascript/_common/auth.js index 41c4d303068..baf79d33fac 100644 --- a/src/javascript/_common/auth.js +++ b/src/javascript/_common/auth.js @@ -7,7 +7,7 @@ const { } = require('@deriv-com/utils'); const Cookies = require('js-cookie'); const requestOidcAuthentication = require('@deriv-com/auth-client').requestOidcAuthentication; -const handlePostLogout = require('@deriv-com/analytics').handlePostLogout; +const OAuth2Logout = require('@deriv-com/auth-client').OAuth2Logout; const Analytics = require('./analytics'); export const DEFAULT_OAUTH_LOGOUT_URL = 'https://oauth.deriv.com/oauth2/sessions/logout'; @@ -79,10 +79,48 @@ export const isOAuth2Enabled = () => { }; export const getLogoutHandler = onWSLogoutAndRedirect => { - const oAuth2Logout = handlePostLogout(onWSLogoutAndRedirect); + const oAuth2Logout = OAuth2Logout(onWSLogoutAndRedirect); return oAuth2Logout; }; +export const requestSingleLogout = async (onWSLogoutAndRedirect) => { + const _requestSingleLogout = async () => { + const isLoggedOutCookie = Cookies.get('logged_state') === 'false'; + const clientAccounts = JSON.parse(localStorage.getItem('client.accounts') || '{}'); + const isClientAccountsPopulated = Object.keys(clientAccounts).length > 0; + const isAuthEnabled = isOAuth2Enabled(); + const isCallbackPage = window.location.pathname.includes('callback'); + const isEndpointPage = window.location.pathname.includes('endpoint'); + + if (isLoggedOutCookie && isClientAccountsPopulated && isAuthEnabled && !isCallbackPage && !isEndpointPage) { + await getLogoutHandler(onWSLogoutAndRedirect) + } + }; + + const isGrowthbookLoaded = Analytics.isGrowthbookLoaded(); + if (!isGrowthbookLoaded) { + let retryInterval = 0; + // this interval is to check if Growthbook is already initialised. + // If not, keep checking it (max 10 times) and SSO if conditions are met + const interval = setInterval(() => { + if (retryInterval > 10) { + clearInterval(interval); + } else { + const isLoaded = Analytics.isGrowthbookLoaded(); + if (isLoaded) { + _requestSingleLogout(); + clearInterval(interval); + } else { + retryInterval += 1; + } + } + }, 500); + } else { + _requestSingleLogout(); + } +}; + + export const requestSingleSignOn = async () => { const _requestSingleSignOn = async () => { // if we have previously logged in, diff --git a/src/javascript/app/base/page.js b/src/javascript/app/base/page.js index 89136eab72a..45ae4e30770 100644 --- a/src/javascript/app/base/page.js +++ b/src/javascript/app/base/page.js @@ -22,7 +22,7 @@ const scrollToTop = require('../../_common/scroll').scrollToTop; const toISOFormat = require('../../_common/string_util').toISOFormat; const Url = require('../../_common/url'); const Analytics = require('../../_common/analytics'); -const { requestSingleSignOn } = require('../../_common/auth'); +const { requestSingleSignOn, requestSingleLogout } = require('../../_common/auth'); const Chat = require('../../_common/chat.js').default; const createElement = require('../../_common/utility').createElement; const isLoginPages = require('../../_common/utility').isLoginPages; @@ -101,7 +101,10 @@ const Page = (() => { updateLinksURL('#content'); } else { init(); + // if the user has logged in previously, silent login requestSingleSignOn(); + // if the user has logged out previously, silent logout + requestSingleLogout(Client.sendLogoutRequest); if (!isLoginPages()) { Language.setCookie(Language.urlLang()); const url_query_strings = Url.paramsHash();