-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from thisyahlen-deriv/thisyahlen/auth2-hook
chore: add useoauth2 hook
- Loading branch information
Showing
5 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useOAuth2 } from './useOAuth2'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './context/'; | ||
export * from './hooks/'; |