Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adrienne / Fix SSO triggering multiple times due to useeffect dependencies #18114

Merged
merged 20 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e5a7364
feat: store redirect metadata for traders hub from os-redirect
adrienne-deriv Jan 24, 2025
833d381
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Jan 24, 2025
8104fe4
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Feb 6, 2025
e9e8b1c
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Feb 7, 2025
5829790
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Feb 7, 2025
cc913a2
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Feb 12, 2025
d16383e
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Feb 18, 2025
b5e1318
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Feb 21, 2025
28e2743
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Feb 21, 2025
8820e8e
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Mar 3, 2025
3f15e54
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Mar 3, 2025
068f03d
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Mar 5, 2025
68533e5
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Mar 6, 2025
e82d39b
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Mar 6, 2025
9dfaa01
Merge branch 'master' of github.com:deriv-com/deriv-app
adrienne-deriv Mar 7, 2025
5b769b8
chore: refactored sso and slo conditions
adrienne-deriv Mar 7, 2025
f10fa88
chore: remove debouncing in sso and slo handler
adrienne-deriv Mar 7, 2025
330bb6e
adrienne-deriv Mar 7, 2025
dde7426
adrienne-deriv Mar 7, 2025
3e83b8d
chore: resolve test cases
adrienne-deriv Mar 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/core/src/App/AppContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const AppContent: React.FC<{ passthrough: unknown }> = observer(({ passthrough }
is_client_store_initialized,
isOAuth2Enabled,
oAuthLogout,
prevent_single_login,
});

const [isWebPasskeysFFEnabled, isGBLoaded] = useGrowthbookIsOn({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ const RootComponent = observer(props => {
setIsWalletsOnboardingTourGuideVisible,
notification_messages_ui,
} = ui;
const { has_wallet, logout, prevent_redirect_to_hub, is_client_store_initialized, setPreventSingleLogin } = client;
const {
has_wallet,
logout,
prevent_redirect_to_hub,
is_client_store_initialized,
prevent_single_login,
setPreventSingleLogin,
} = client;

const { oAuthLogout } = useOauth2({ handleLogout: logout });

Expand Down Expand Up @@ -56,14 +63,15 @@ const RootComponent = observer(props => {
}

const shouldStayInDerivApp = !isHubRedirectionEnabled || !has_wallet || prevent_redirect_to_hub;
if (isHubRedirectionLoaded && is_client_store_initialized && shouldStayInDerivApp) {
if (prevent_single_login && isHubRedirectionLoaded && is_client_store_initialized && shouldStayInDerivApp) {
setPreventSingleLogin(false);
}
}, [
isHubRedirectionLoaded,
isHubRedirectionEnabled,
has_wallet,
prevent_redirect_to_hub,
prevent_single_login,
is_client_store_initialized,
]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react';
import Cookies from 'js-cookie';

import { requestOidcAuthentication } from '@deriv-com/auth-client';
import { renderHook } from '@testing-library/react-hooks';

import useSilentLoginAndLogout from '../useSilentLoginAndLogout';
import { mockStore, StoreProvider } from '@deriv/stores';

jest.mock('js-cookie', () => ({
get: jest.fn(),
Expand All @@ -15,6 +17,12 @@ jest.mock('@deriv-com/auth-client', () => ({

describe('useSilentLoginAndLogout', () => {
const mockOAuthLogout = jest.fn();
const mockStoreData = mockStore({
client: { prevent_single_login: false },
});
const wrapper = ({ children }: { children: JSX.Element }) => (
<StoreProvider store={mockStoreData}>{children}</StoreProvider>
);

beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -42,12 +50,14 @@ describe('useSilentLoginAndLogout', () => {

jest.spyOn(Storage.prototype, 'getItem').mockReturnValue(JSON.stringify({}));

renderHook(() =>
useSilentLoginAndLogout({
is_client_store_initialized: true,
isOAuth2Enabled: true,
oAuthLogout: mockOAuthLogout,
})
renderHook(
() =>
useSilentLoginAndLogout({
is_client_store_initialized: true,
isOAuth2Enabled: true,
oAuthLogout: mockOAuthLogout,
}),
{ wrapper }
);

expect(requestOidcAuthentication).toHaveBeenCalledWith({
Expand All @@ -61,12 +71,14 @@ describe('useSilentLoginAndLogout', () => {

jest.spyOn(Storage.prototype, 'getItem').mockReturnValue(JSON.stringify({}));

renderHook(() =>
useSilentLoginAndLogout({
is_client_store_initialized: true,
isOAuth2Enabled: true,
oAuthLogout: mockOAuthLogout,
})
renderHook(
() =>
useSilentLoginAndLogout({
is_client_store_initialized: true,
isOAuth2Enabled: true,
oAuthLogout: mockOAuthLogout,
}),
{ wrapper }
);

expect(requestOidcAuthentication).not.toHaveBeenCalled();
Expand All @@ -83,12 +95,14 @@ describe('useSilentLoginAndLogout', () => {
value: { pathname: '/callback' },
});

renderHook(() =>
useSilentLoginAndLogout({
is_client_store_initialized: true,
isOAuth2Enabled: true,
oAuthLogout: mockOAuthLogout,
})
renderHook(
() =>
useSilentLoginAndLogout({
is_client_store_initialized: true,
isOAuth2Enabled: true,
oAuthLogout: mockOAuthLogout,
}),
{ wrapper }
);

expect(requestOidcAuthentication).not.toHaveBeenCalled();
Expand All @@ -100,12 +114,14 @@ describe('useSilentLoginAndLogout', () => {

jest.spyOn(Storage.prototype, 'getItem').mockReturnValue(JSON.stringify({ account1: {}, account2: {} }));

renderHook(() =>
useSilentLoginAndLogout({
is_client_store_initialized: true,
isOAuth2Enabled: true,
oAuthLogout: mockOAuthLogout,
})
renderHook(
() =>
useSilentLoginAndLogout({
is_client_store_initialized: true,
isOAuth2Enabled: true,
oAuthLogout: mockOAuthLogout,
}),
{ wrapper }
);

expect(requestOidcAuthentication).not.toHaveBeenCalled();
Expand All @@ -117,12 +133,14 @@ describe('useSilentLoginAndLogout', () => {

jest.spyOn(Storage.prototype, 'getItem').mockReturnValue(JSON.stringify({ account1: {}, account2: {} }));

renderHook(() =>
useSilentLoginAndLogout({
is_client_store_initialized: true,
isOAuth2Enabled: true,
oAuthLogout: mockOAuthLogout,
})
renderHook(
() =>
useSilentLoginAndLogout({
is_client_store_initialized: true,
isOAuth2Enabled: true,
oAuthLogout: mockOAuthLogout,
}),
{ wrapper }
);

expect(requestOidcAuthentication).not.toHaveBeenCalled();
Expand Down
37 changes: 15 additions & 22 deletions packages/hooks/src/useSilentLoginAndLogout.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';
import Cookies from 'js-cookie';

import { requestOidcAuthentication } from '@deriv-com/auth-client';

import { useStore } from '@deriv/stores';
/**
* Handles silent login and single logout logic for OAuth2.
*
Expand All @@ -16,60 +16,53 @@ const useSilentLoginAndLogout = ({
is_client_store_initialized,
isOAuth2Enabled,
oAuthLogout,
prevent_single_login,
}: {
is_client_store_initialized: boolean;
isOAuth2Enabled: boolean;
oAuthLogout: () => Promise<void>;
prevent_single_login?: boolean;
}) => {
const loggedState = Cookies.get('logged_state');

const { client } = useStore();
const clientAccounts = JSON.parse(localStorage.getItem('client.accounts') || '{}');
const isClientAccountsPopulated = Object.keys(clientAccounts).length > 0;
const isSilentLoginExcluded =
window.location.pathname.includes('callback') || window.location.pathname.includes('endpoint');

// state to manage and ensure OIDC callback functions are invoked once only
const isAuthenticating = useRef(false);
const isLoggingOut = useRef(false);
const { prevent_single_login } = client;

useEffect(() => {
if (prevent_single_login) return;
if (prevent_single_login || !isOAuth2Enabled || !is_client_store_initialized || isSilentLoginExcluded) 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');
if (isUsingLegacyFlow && loggedState === 'false' && isOAuth2Enabled) {
return;
}

if (
!isUsingLegacyFlow &&
loggedState === 'true' &&
!isClientAccountsPopulated &&
isOAuth2Enabled &&
is_client_store_initialized &&
!isSilentLoginExcluded
) {
if (!isUsingLegacyFlow && loggedState === 'true' && !isClientAccountsPopulated) {
// Perform silent login
if (isAuthenticating.current) return;
isAuthenticating.current = true;
requestOidcAuthentication({
redirectCallbackUri: `${window.location.origin}/callback`,
});
}

if (
!isUsingLegacyFlow &&
loggedState === 'false' &&
is_client_store_initialized &&
isOAuth2Enabled &&
isClientAccountsPopulated &&
!window.location.pathname.includes('callback')
) {
if (!isUsingLegacyFlow && loggedState === 'false' && isClientAccountsPopulated) {
// Perform single logout
if (isLoggingOut.current) return;
isLoggingOut.current = true;
oAuthLogout();
}
}, [
loggedState,
isClientAccountsPopulated,
is_client_store_initialized,
isOAuth2Enabled,
oAuthLogout,
isSilentLoginExcluded,
prevent_single_login,
]);
Expand Down
Loading