Skip to content

Commit

Permalink
chore: used oauth2logout as logout function
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienne-deriv committed Dec 9, 2024
1 parent 1091d48 commit 769cade
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
42 changes: 40 additions & 2 deletions src/javascript/_common/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 () => {

Check warning on line 87 in src/javascript/_common/auth.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

Unexpected dangling '_' in '_requestSingleLogout'

Check warning on line 87 in src/javascript/_common/auth.js

View workflow job for this annotation

GitHub Actions / Build and Test

Unexpected dangling '_' in '_requestSingleLogout'
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)

Check failure on line 96 in src/javascript/_common/auth.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

Missing semicolon

Check failure on line 96 in src/javascript/_common/auth.js

View workflow job for this annotation

GitHub Actions / Build and Test

Missing semicolon
}
};

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();
}
};


Check failure on line 123 in src/javascript/_common/auth.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

More than 1 blank line not allowed

Check failure on line 123 in src/javascript/_common/auth.js

View workflow job for this annotation

GitHub Actions / Build and Test

More than 1 blank line not allowed
export const requestSingleSignOn = async () => {
const _requestSingleSignOn = async () => {

Check warning on line 125 in src/javascript/_common/auth.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

Unexpected dangling '_' in '_requestSingleSignOn'

Check warning on line 125 in src/javascript/_common/auth.js

View workflow job for this annotation

GitHub Actions / Build and Test

Unexpected dangling '_' in '_requestSingleSignOn'
// if we have previously logged in,
Expand Down
5 changes: 4 additions & 1 deletion src/javascript/app/base/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 769cade

Please sign in to comment.