Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move the logout to a function and remove the GB check #115

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
};
};
Loading