-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
753bb65
commit de69d69
Showing
5 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,85 @@ | ||
const Utils = require('@deriv-com/utils'); | ||
|
||
export const DEFAULT_OAUTH_LOGOUT_URL = 'https://oauth.deriv.com/oauth2/sessions/logout'; | ||
|
||
export const DEFAULT_OAUTH_ORIGIN_URL = 'https://oauth.deriv.com'; | ||
|
||
export const getOAuthLogoutUrl = () => { | ||
const { appId, serverUrl } = Utils.WebSocketUtils.getServerInfo(); | ||
|
||
const oauthUrl = appId && serverUrl ? `https://${serverUrl}/oauth2/sessions/logout` : DEFAULT_OAUTH_LOGOUT_URL; | ||
|
||
return oauthUrl; | ||
}; | ||
|
||
export const getOAuthOrigin = () => { | ||
const { appId, serverUrl } = Utils.WebSocketUtils.getServerInfo(); | ||
|
||
const oauthUrl = appId && serverUrl ? `https://${serverUrl}` : DEFAULT_OAUTH_ORIGIN_URL; | ||
|
||
return oauthUrl; | ||
}; | ||
|
||
export const AuthClient = (() => { | ||
const isOAuth2Enabled = oAuth2GrowthbookConfig => { | ||
const { OAuth2EnabledApps, OAuth2EnabledAppsInitialised } = oAuth2GrowthbookConfig; | ||
const appId = Utils.WebSocketUtils.getAppId(); | ||
|
||
if (OAuth2EnabledAppsInitialised) { | ||
const FEHydraAppIds = OAuth2EnabledApps?.length | ||
? OAuth2EnabledApps[OAuth2EnabledApps.length - 1]?.enabled_for ?? [] | ||
: []; | ||
return FEHydraAppIds.includes(+appId); | ||
} | ||
|
||
return false; | ||
}; | ||
const getLogoutHandler = (oAuth2GrowthbookConfig, onWSLogoutAndRedirect) => { | ||
const isAuthEnabled = isOAuth2Enabled(oAuth2GrowthbookConfig); | ||
|
||
if (!isAuthEnabled) { | ||
console.log('lol auth2 is not enabled'); | ||
Check failure on line 41 in src/javascript/_common/auth.js GitHub Actions / build_and_deploy_preview_link
|
||
return onWSLogoutAndRedirect; | ||
} | ||
|
||
const onMessage = async event => { | ||
const allowedOrigin = getOAuthOrigin(); | ||
if (allowedOrigin === event.origin) { | ||
if (event.data === 'logout_complete') { | ||
console.log('logout_complete calledd!'); | ||
Check failure on line 49 in src/javascript/_common/auth.js GitHub Actions / build_and_deploy_preview_link
|
||
onWSLogoutAndRedirect(); | ||
} | ||
} | ||
}; | ||
|
||
window.addEventListener('message', onMessage); | ||
|
||
const oAuth2Logout = async () => { | ||
if (!isAuthEnabled) { | ||
console.log('lol auth2 is not enabled'); | ||
Check failure on line 59 in src/javascript/_common/auth.js GitHub Actions / build_and_deploy_preview_link
|
||
onWSLogoutAndRedirect(); | ||
return; | ||
} | ||
|
||
let iframe = document.getElementById('logout-iframe'); | ||
if (!iframe) { | ||
iframe = document.createElement('iframe'); | ||
iframe.id = 'logout-iframe'; | ||
iframe.style.display = 'none'; | ||
document.body.appendChild(iframe); | ||
|
||
setTimeout(() => { | ||
onWSLogoutAndRedirect(); | ||
}, 10000); | ||
} | ||
console.log('auth2 doneee'); | ||
Check failure on line 75 in src/javascript/_common/auth.js GitHub Actions / build_and_deploy_preview_link
|
||
}; | ||
|
||
return oAuth2Logout; | ||
}; | ||
|
||
return { | ||
getLogoutHandler, | ||
isOAuth2Enabled, | ||
}; | ||
})(); |
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,57 @@ | ||
// growthbook.js | ||
import { useState, useEffect } from 'react'; | ||
|
||
import { Analytics } from '@deriv-com/analytics'; | ||
|
||
export const useIsGrowthbookIsLoaded = () => { | ||
const [isGBLoaded, setIsGBLoaded] = useState(false); | ||
|
||
useEffect(() => { | ||
let checksCounter = 0; | ||
const analyticsInterval = setInterval(() => { | ||
// Check if the analytics instance is available for 10 seconds before setting the feature flag value | ||
if (checksCounter > 20) { | ||
// If the analytics instance is not available after 10 seconds, clear the interval | ||
clearInterval(analyticsInterval); | ||
return; | ||
} | ||
checksCounter += 1; | ||
if (Analytics?.getInstances()?.ab) { | ||
setIsGBLoaded(true); | ||
clearInterval(analyticsInterval); | ||
} | ||
}, 500); | ||
|
||
return () => { | ||
clearInterval(analyticsInterval); | ||
}; | ||
}, []); | ||
|
||
return isGBLoaded; | ||
}; | ||
|
||
export const useGrowthbookGetFeatureValue = ({ defaultValue, featureFlag }) => { | ||
const resolvedDefaultValue = defaultValue !== undefined ? defaultValue : false; | ||
const [featureFlagValue, setFeatureFlagValue] = useState( | ||
Analytics?.getFeatureValue(featureFlag, resolvedDefaultValue) ?? resolvedDefaultValue | ||
); | ||
const isGBLoaded = useIsGrowthbookIsLoaded(); | ||
|
||
useEffect(() => { | ||
if (isGBLoaded) { | ||
if (Analytics?.getInstances()?.ab) { | ||
const setFeatureValue = () => { | ||
const value = Analytics?.getFeatureValue(featureFlag, resolvedDefaultValue); | ||
setFeatureFlagValue(value); | ||
}; | ||
setFeatureValue(); | ||
Analytics?.getInstances()?.ab?.GrowthBook?.setRenderer(() => { | ||
// this will be called whenever the feature flag value changes and acts as a event listener | ||
setFeatureValue(); | ||
}); | ||
} | ||
} | ||
}, [isGBLoaded, resolvedDefaultValue, featureFlag]); | ||
|
||
return [featureFlagValue, isGBLoaded]; | ||
}; |