Skip to content

Commit

Permalink
Merge pull request #54 from thisyahlen-deriv/thisyahlen/cleanup-iframe
Browse files Browse the repository at this point in the history
chore: remove iframe once logout is completed
  • Loading branch information
thisyahlen-deriv authored Oct 1, 2024
2 parents 807a3e6 + eed337f commit cdc05e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/hooks/useOAuth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type OAuth2GBConfig = {
OAuth2EnabledAppsInitialised: boolean;
};

const LOGOUT_TIMEOUT = 10000;

/**
* Custom hook to handle OAuth2 logout and redirection.
*
Expand All @@ -29,18 +31,21 @@ export const useOAuth2 = (OAuth2GrowthBookConfig: OAuth2GBConfig, WSLogoutAndRed

const timeout = useRef<ReturnType<typeof setTimeout>>();

const cleanup = () => {
clearTimeout(timeout.current);

const iframe = document.getElementById('logout-iframe') as HTMLIFrameElement;
if (iframe) iframe.remove();
};

const OAuth2Logout = useCallback(async () => {
if (!isOAuth2Enabled) {
WSLogoutAndRedirect();
return;
}
if (!isOAuth2Enabled) return WSLogoutAndRedirect();

const onMessage = (event: MessageEvent) => {
if (event.data === 'logout_complete') {
console.log('logout', event.data);
WSLogoutAndRedirect();
window.removeEventListener('message', onMessage);
clearTimeout(timeout.current);
cleanup();
}
};
window.addEventListener('message', onMessage);
Expand All @@ -54,13 +59,17 @@ export const useOAuth2 = (OAuth2GrowthBookConfig: OAuth2GBConfig, WSLogoutAndRed

timeout.current = setTimeout(() => {
WSLogoutAndRedirect();
}, 10000);
window.removeEventListener('message', onMessage);
cleanup();
}, LOGOUT_TIMEOUT);
}

iframe.src = getOAuthLogoutUrl();

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

Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './context/';
export * from './hooks/';

0 comments on commit cdc05e1

Please sign in to comment.