Skip to content

Commit

Permalink
Merge pull request #115 from thisyahlen-deriv/thisyahlen/remove-gb-ch…
Browse files Browse the repository at this point in the history
…eck-for-logout

chore: move the logout to a function and remove the GB check
  • Loading branch information
thisyahlen-deriv authored Dec 3, 2024
2 parents a341f45 + 0a796e9 commit 8bca9c6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/hooks/useOAuth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const LOGOUT_TIMEOUT = 10000;
* @param {OAuth2Config} config - Configuration object containing OAuth2 enabled apps flag and initialisation flag.
* @param {(oauthUrl: string) => Promise<void>} WSLogoutAndRedirect - Function to handle logout and redirection.
* @returns {{ OAuth2Logout: () => Promise<void> }} - Object containing the OAuth2Logout function.
* @deprecated Please use OAuth2Logout function instead of this hook from the `@deriv-com/auth-client` package.
*/
export const useOAuth2 = (OAuth2GrowthBookConfig: OAuth2GBConfig, WSLogoutAndRedirect: () => Promise<void>) => {
const { OAuth2EnabledApps, OAuth2EnabledAppsInitialised } = OAuth2GrowthBookConfig;
Expand Down
51 changes: 50 additions & 1 deletion src/oidc/oidc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { UserManager, WebStorageStateStore } from 'oidc-client-ts';
import { OIDCError, OIDCErrorType } from './error';
import { getServerInfo } from '../constants';
import { DEFAULT_OAUTH_LOGOUT_URL, getServerInfo } from '../constants';
import { getConfigurations } from './config';
import Cookies from 'js-cookie';

export type OidcConfiguration = {
issuer: string;
Expand Down Expand Up @@ -262,3 +263,51 @@ export const createUserManager = async (options: CreateUserManagerOptions) => {
throw new OIDCError(OIDCErrorType.UserManagerCreationFailed, 'unable to create user manager for OIDC');
}
};

/**
* Logs out the user from the auth server and calls the callback function when the logout is complete.
* @param WSLogoutAndRedirect - The callback function to call after the logout is complete
*/
export const OAuth2Logout = (WSLogoutAndRedirect: () => void) => {
const oidcEndpoints = localStorage.getItem('config.oidc_endpoints') || '{}';

const logoutUrl = JSON.parse(oidcEndpoints).end_session_endpoint || DEFAULT_OAUTH_LOGOUT_URL;
const cleanup = () => {
const iframe = document.getElementById('logout-iframe') as HTMLIFrameElement;
if (iframe) iframe.remove();
};
const onMessage = (event: MessageEvent) => {
if (event.data === 'logout_complete') {
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();
cleanup();
window.removeEventListener('message', onMessage);
}
};

window.addEventListener('message', onMessage);

let iframe: HTMLIFrameElement | null = document.getElementById('logout-iframe') as HTMLIFrameElement;
if (!iframe) {
iframe = document.createElement('iframe');
iframe.id = 'logout-iframe';
iframe.style.display = 'none';
document.body.appendChild(iframe);
}

iframe.src = logoutUrl;

iframe.onerror = error => {
console.error('There has been a problem with the logout: ', error);
cleanup();
};
};

0 comments on commit 8bca9c6

Please sign in to comment.