Skip to content

Commit

Permalink
Merge pull request #24 from thisyahlen-deriv/thisyahlen/auth2-hook
Browse files Browse the repository at this point in the history
chore: add useoauth2 hook
  • Loading branch information
thisyahlen-deriv authored Sep 12, 2024
2 parents 99905fe + 46638f6 commit d75aee2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/context/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { OAuth2Provider } from './auth-context';
export { useOAuth2 } from './use-oauth2';
export { useOAuth2Context } from './use-oauth2';
2 changes: 1 addition & 1 deletion src/context/use-oauth2.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from 'react';
import { OAuth2Context } from '../context/auth-context';

export const useOAuth2 = () => {
export const useOAuth2Context = () => {
const context = useContext(OAuth2Context);
if (!context) {
throw new Error('useOAuth2 must be used within an OAuth2Provider');
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useOAuth2 } from './useOAuth2';
55 changes: 55 additions & 0 deletions src/hooks/useOAuth2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useEffect, useCallback } from 'react';
import { getOAuthLogoutUrl, getOAuthOrigin } from '../constants/';

type MessageEvent = {
data: 'logout_complete' | 'logout_error';
origin: string;
};

/**
* Custom hook to handle OAuth2 logout and redirection.
*
* @param {(oauthUrl: string) => Promise<void>} WSLogoutAndRedirect - Function to handle logout and redirection.
* @returns {{ OAuth2Logout: () => Promise<void> }} - Object containing the OAuth2Logout function.
*/
export const useOAuth2 = (WSLogoutAndRedirect: () => Promise<void>) => {
useEffect(() => {
const onMessage = async (event: MessageEvent) => {
const allowedOrigin = getOAuthOrigin();
if (allowedOrigin === event.origin) {
if (event.data === 'logout_complete') {
WSLogoutAndRedirect();
} else {
console.warn('Unexpected message received: ', event.data);
}
} else {
console.warn('Unexpected postmessage origin: ', event.origin);
}
};

window.addEventListener('message', onMessage);
return () => window.removeEventListener('message', onMessage);
}, [WSLogoutAndRedirect]);

const OAuth2Logout = useCallback(async () => {
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);

setTimeout(() => {
WSLogoutAndRedirect();
}, 10000);
}

iframe.src = getOAuthLogoutUrl();

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

return { OAuth2Logout };
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './context/';
export * from './hooks/';

0 comments on commit d75aee2

Please sign in to comment.