Skip to content

Commit b511b8b

Browse files
chore: add gb logic and logout logic in lib level
1 parent d75aee2 commit b511b8b

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/hooks/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { useOAuth2 } from './useOAuth2';
2+
export { useIsOAuth2Enabled } from './useIsOAuth2Enabled';

src/hooks/useIsOAuth2Enabled.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useState, useEffect } from 'react';
2+
import { getServerInfo } from '../constants/';
3+
4+
type hydraBEApps = {
5+
enabled_for: number[];
6+
};
7+
8+
export const useIsOAuth2Enabled = (
9+
OAuth2EnabledApps: hydraBEApps[],
10+
OAuth2EnabledAppsInitialised: boolean
11+
): boolean => {
12+
const [isOAuth2Enabled, setIsOAuth2Enabled] = useState<boolean>(false);
13+
const { appId } = getServerInfo();
14+
15+
useEffect(() => {
16+
if (OAuth2EnabledAppsInitialised) {
17+
const FEHydraAppIds = OAuth2EnabledApps.length
18+
? (OAuth2EnabledApps[OAuth2EnabledApps.length - 1]?.enabled_for ?? [])
19+
: [];
20+
setIsOAuth2Enabled(FEHydraAppIds.includes(+(appId as string)));
21+
}
22+
}, [OAuth2EnabledAppsInitialised, OAuth2EnabledApps, appId]);
23+
24+
return isOAuth2Enabled;
25+
};

src/hooks/useOAuth2.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
import { useEffect, useCallback } from 'react';
22
import { getOAuthLogoutUrl, getOAuthOrigin } from '../constants/';
3+
import { useIsOAuth2Enabled } from './useIsOAuth2Enabled';
34

45
type MessageEvent = {
56
data: 'logout_complete' | 'logout_error';
67
origin: string;
78
};
89

10+
type hydraBEApps = {
11+
enabled_for: number[];
12+
};
13+
14+
type OAuth2Config = {
15+
OAuth2EnabledApps: hydraBEApps[];
16+
OAuth2EnabledAppsInitialised: boolean;
17+
};
18+
919
/**
1020
* Custom hook to handle OAuth2 logout and redirection.
1121
*
22+
* @param {OAuth2Config} config - Configuration object containing OAuth2 enabled apps and initialisation flag.
1223
* @param {(oauthUrl: string) => Promise<void>} WSLogoutAndRedirect - Function to handle logout and redirection.
1324
* @returns {{ OAuth2Logout: () => Promise<void> }} - Object containing the OAuth2Logout function.
1425
*/
15-
export const useOAuth2 = (WSLogoutAndRedirect: () => Promise<void>) => {
26+
export const useOAuth2 = (OAuth2GrowthBookConfig: OAuth2Config, WSLogoutAndRedirect: () => Promise<void>) => {
27+
const { OAuth2EnabledApps, OAuth2EnabledAppsInitialised } = OAuth2GrowthBookConfig;
28+
const isOAuth2Enabled = useIsOAuth2Enabled(OAuth2EnabledApps, OAuth2EnabledAppsInitialised);
29+
1630
useEffect(() => {
31+
if (!isOAuth2Enabled) return;
32+
1733
const onMessage = async (event: MessageEvent) => {
1834
const allowedOrigin = getOAuthOrigin();
1935
if (allowedOrigin === event.origin) {
@@ -29,9 +45,14 @@ export const useOAuth2 = (WSLogoutAndRedirect: () => Promise<void>) => {
2945

3046
window.addEventListener('message', onMessage);
3147
return () => window.removeEventListener('message', onMessage);
32-
}, [WSLogoutAndRedirect]);
48+
}, [isOAuth2Enabled, WSLogoutAndRedirect]);
3349

3450
const OAuth2Logout = useCallback(async () => {
51+
if (!isOAuth2Enabled) {
52+
WSLogoutAndRedirect();
53+
return;
54+
}
55+
3556
let iframe: HTMLIFrameElement | null = document.getElementById('logout-iframe') as HTMLIFrameElement;
3657
if (!iframe) {
3758
iframe = document.createElement('iframe');
@@ -49,7 +70,7 @@ export const useOAuth2 = (WSLogoutAndRedirect: () => Promise<void>) => {
4970
iframe.onerror = error => {
5071
console.error('There has been a problem with the logout: ', error);
5172
};
52-
}, [WSLogoutAndRedirect]);
73+
}, [isOAuth2Enabled, WSLogoutAndRedirect]);
5374

5475
return { OAuth2Logout };
5576
};

0 commit comments

Comments
 (0)