From c72eaf66c9be8c48084f149c8f97d615f683132b Mon Sep 17 00:00:00 2001 From: adrienne-deriv <103016120+adrienne-deriv@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:30:15 +0800 Subject: [PATCH] Adrienne / Fix SSO triggering twice before redirecting to Traders Hub (#18109) * feat: store redirect metadata for traders hub from os-redirect * Merge branch 'master' of github.com:deriv-com/deriv-app * chore: wait for traders hub redirection to check before sso kicks in * chore: wait for feature flag redirection to be loaded * chore: fixed typing --- packages/core/src/App/AppContent.tsx | 2 ++ .../RootComponent/root-component.jsx | 21 ++++++++++++++++--- packages/core/src/Stores/client-store.js | 6 ++++++ .../hooks/src/useIsHubRedirectionEnabled.ts | 4 ++-- packages/hooks/src/useSilentLoginAndLogout.ts | 4 ++++ packages/stores/types.ts | 2 ++ 6 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/core/src/App/AppContent.tsx b/packages/core/src/App/AppContent.tsx index 343ace08d689..85d5ebcb9683 100644 --- a/packages/core/src/App/AppContent.tsx +++ b/packages/core/src/App/AppContent.tsx @@ -50,6 +50,7 @@ const AppContent: React.FC<{ passthrough: unknown }> = observer(({ passthrough } setIsPhoneNumberVerificationEnabled, setIsCountryCodeDropdownEnabled, accounts, + prevent_single_login, } = store.client; const { first_name, last_name } = account_settings; const { current_language, changeSelectedLanguage } = store.common; @@ -72,6 +73,7 @@ const AppContent: React.FC<{ passthrough: unknown }> = observer(({ passthrough } is_client_store_initialized, isOAuth2Enabled, oAuthLogout, + prevent_single_login, }); const [isWebPasskeysFFEnabled, isGBLoaded] = useGrowthbookIsOn({ diff --git a/packages/core/src/App/Containers/RootComponent/root-component.jsx b/packages/core/src/App/Containers/RootComponent/root-component.jsx index 55fa4783646f..b16740822c5d 100644 --- a/packages/core/src/App/Containers/RootComponent/root-component.jsx +++ b/packages/core/src/App/Containers/RootComponent/root-component.jsx @@ -25,18 +25,22 @@ const RootComponent = observer(props => { setIsWalletsOnboardingTourGuideVisible, notification_messages_ui, } = ui; - const { has_wallet, logout, prevent_redirect_to_hub, is_client_store_initialized } = client; + const { has_wallet, logout, prevent_redirect_to_hub, is_client_store_initialized, setPreventSingleLogin } = client; const { oAuthLogout } = useOauth2({ handleLogout: logout }); const onWalletsOnboardingTourGuideCloseHandler = () => { setIsWalletsOnboardingTourGuideVisible(false); }; - const { isHubRedirectionEnabled } = useIsHubRedirectionEnabled(); + const { isHubRedirectionEnabled, isHubRedirectionLoaded } = useIsHubRedirectionEnabled(); const PRODUCTION_REDIRECT_URL = 'https://hub.deriv.com/tradershub/home'; const STAGING_REDIRECT_URL = 'https://staging-hub.deriv.com/tradershub/home'; + useEffect(() => { + setPreventSingleLogin(true); + }, []); + useEffect(() => { if (isHubRedirectionEnabled && has_wallet && !prevent_redirect_to_hub && is_client_store_initialized) { const redirectUrl = process.env.NODE_ENV === 'production' ? PRODUCTION_REDIRECT_URL : STAGING_REDIRECT_URL; @@ -50,7 +54,18 @@ const RootComponent = observer(props => { localStorage.removeItem('active_wallet_loginid'); window.location.assign(redirectUrl); } - }, [isHubRedirectionEnabled, has_wallet, prevent_redirect_to_hub, is_client_store_initialized]); + + const shouldStayInDerivApp = !isHubRedirectionEnabled || !has_wallet || prevent_redirect_to_hub; + if (isHubRedirectionLoaded && is_client_store_initialized && shouldStayInDerivApp) { + setPreventSingleLogin(false); + } + }, [ + isHubRedirectionLoaded, + isHubRedirectionEnabled, + has_wallet, + prevent_redirect_to_hub, + is_client_store_initialized, + ]); return has_wallet ? ( { + this.prevent_single_login = value; + }; + getIsMarketTypeMatching = (account, market_type) => { if (market_type === 'synthetic') { return account.market_type === market_type || account.market_type === 'gaming'; diff --git a/packages/hooks/src/useIsHubRedirectionEnabled.ts b/packages/hooks/src/useIsHubRedirectionEnabled.ts index 43cce14f9445..0b2969bce962 100644 --- a/packages/hooks/src/useIsHubRedirectionEnabled.ts +++ b/packages/hooks/src/useIsHubRedirectionEnabled.ts @@ -7,7 +7,7 @@ type THubEnabledCountryList = { }; const useIsHubRedirectionEnabled = () => { - const [hubEnabledCountryList] = useGrowthbookGetFeatureValue({ + const [hubEnabledCountryList, isHubRedirectionLoaded] = useGrowthbookGetFeatureValue({ featureFlag: 'hub_enabled_country_list', }); const { client } = useStore(); @@ -29,7 +29,7 @@ const useIsHubRedirectionEnabled = () => { clients_country && (hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(clients_country); - return { isHubRedirectionEnabled, isChangingToHubAppId }; + return { isHubRedirectionEnabled, isChangingToHubAppId, isHubRedirectionLoaded }; }; export default useIsHubRedirectionEnabled; diff --git a/packages/hooks/src/useSilentLoginAndLogout.ts b/packages/hooks/src/useSilentLoginAndLogout.ts index 284a007578cc..591887f2f321 100644 --- a/packages/hooks/src/useSilentLoginAndLogout.ts +++ b/packages/hooks/src/useSilentLoginAndLogout.ts @@ -16,10 +16,12 @@ const useSilentLoginAndLogout = ({ is_client_store_initialized, isOAuth2Enabled, oAuthLogout, + prevent_single_login, }: { is_client_store_initialized: boolean; isOAuth2Enabled: boolean; oAuthLogout: () => Promise; + prevent_single_login?: boolean; }) => { const loggedState = Cookies.get('logged_state'); @@ -29,6 +31,7 @@ const useSilentLoginAndLogout = ({ window.location.pathname.includes('callback') || window.location.pathname.includes('endpoint'); useEffect(() => { + if (prevent_single_login) return; // NOTE: Remove this logic once social signup is intergated with OIDC const params = new URLSearchParams(window.location.search); const isUsingLegacyFlow = params.has('token1') && params.has('acct1'); @@ -68,6 +71,7 @@ const useSilentLoginAndLogout = ({ isOAuth2Enabled, oAuthLogout, isSilentLoginExcluded, + prevent_single_login, ]); }; diff --git a/packages/stores/types.ts b/packages/stores/types.ts index 4b601d36aae9..d79f87439b2a 100644 --- a/packages/stores/types.ts +++ b/packages/stores/types.ts @@ -542,7 +542,9 @@ export type TClientStore = { standpoint: TStandPoint; is_p2p_available: boolean; prevent_redirect_to_hub: boolean; + prevent_single_login: boolean; setPreventRedirectToHub: (value: boolean) => void; + setPreventSingleLogin: (value: boolean) => void; setAccountStatus: (status?: GetAccountStatus) => void; setBalanceOtherAccounts: (balance: number) => void; selectCurrency: (currency: string) => void;