Skip to content

Commit

Permalink
Merge pull request #183 from deriv-com/revert-176-thisyahlen/implemen…
Browse files Browse the repository at this point in the history
…t-oidc-logic

Revert "feat: oidc implementation"
  • Loading branch information
shafin-deriv authored Dec 10, 2024
2 parents 58f0afc + f7bdac1 commit b3930fc
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 249 deletions.
26 changes: 11 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"dependencies": {
"@deriv-com/quill-ui": "^1.16.21",
"@deriv-com/analytics": "^1.22.1",
"@deriv-com/auth-client": "^1.3.1",
"@deriv-com/auth-client": "^1.0.29",
"@deriv/deriv-api": "^1.0.11",
"@radix-ui/react-tooltip": "^1.0.7",
"@react-spring/web": "^9.7.3",
Expand Down
39 changes: 2 additions & 37 deletions src/components/UserNavbarItem/item.desktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import useDeviceType from '@site/src/hooks/useDeviceType';

import { IUserNavbarItemProps } from './item.types';
import styles from './UserNavbarItem.module.scss';
import { useHandleLogin } from '@site/src/hooks/useHandleLogin';
import Cookies from 'js-cookie';

interface IActionProps {
handleClick: () => void;
Expand Down Expand Up @@ -64,16 +62,12 @@ const DashboardActions: React.FC<IActionProps> = ({ handleClick, isDesktop }) =>
const SignedInActions: React.FC<IActionProps> = ({ handleClick, isDesktop }) => {
const signedInButtonClasses = clsx('navbar__item', styles.UserNavbarItem, styles.SignedInButton);

const { handleLogin } = useHandleLogin({
onClickLogin: handleClick,
});

return (
<nav className='right-navigation'>
<Button
variant='secondary'
color='black'
onClick={handleLogin}
onClick={handleClick}
className={signedInButtonClasses}
data-testid='sa_login'
>
Expand All @@ -94,43 +88,14 @@ const SignedInActions: React.FC<IActionProps> = ({ handleClick, isDesktop }) =>
};

const UserNavbarDesktopItem = ({ authUrl, is_logged_in }: IUserNavbarItemProps) => {
const { logout } = useLogout();
const { deviceType } = useDeviceType();
const isDesktop = deviceType === 'desktop';

const handleClick = () => {
location.assign(authUrl);
};

const { handleLogin, isOAuth2Enabled } = useHandleLogin({
onClickLogin: handleClick,
});

const { logout } = useLogout();

const loggedState = Cookies.get('logged_state');

const loginAccountsSessionStorage = JSON.parse(sessionStorage.getItem('login-accounts'));

const isLoginAccountsPopulated =
loginAccountsSessionStorage && loginAccountsSessionStorage.length > 0;

React.useEffect(() => {
if (
loggedState === 'true' &&
isOAuth2Enabled &&
!isLoginAccountsPopulated &&
window.location.pathname !== '/callback'
) {
console.log('isLoginAccountsPopulated', !isLoginAccountsPopulated);

handleLogin();
}

if (loggedState === 'false' && isOAuth2Enabled && isLoginAccountsPopulated) {
logout();
}
}, [isOAuth2Enabled, loggedState, logout, handleLogin, isLoginAccountsPopulated]);

return is_logged_in ? (
<DashboardActions handleClick={logout} isDesktop={isDesktop} />
) : (
Expand Down
7 changes: 1 addition & 6 deletions src/features/Apiexplorer/LoginDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import useLoginUrl from '@site/src/hooks/useLoginUrl';
import styles from './LoginDialog.module.scss';
import Translate, { translate } from '@docusaurus/Translate';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import { useHandleLogin } from '@site/src/hooks/useHandleLogin';

type TLoginDialog = {
setToggleModal: React.Dispatch<React.SetStateAction<boolean>>;
Expand All @@ -26,10 +25,6 @@ export const LoginDialog = ({ setToggleModal }: TLoginDialog) => {
location.assign(getUrl(currentLocale));
};

const { handleLogin } = useHandleLogin({
onClickLogin: handleClick,
});

const handleSignUp = () => {
location.assign('https://deriv.com/signup/');
};
Expand Down Expand Up @@ -62,7 +57,7 @@ export const LoginDialog = ({ setToggleModal }: TLoginDialog) => {
<Button color='tertiary' onClick={handleSignUp} className={styles.btn}>
<Translate>Sign up</Translate>
</Button>
<Button color='primary' onClick={handleLogin} className={styles.btn}>
<Button color='primary' onClick={handleClick} className={styles.btn}>
<Translate>Log in</Translate>
</Button>
</div>
Expand Down
22 changes: 0 additions & 22 deletions src/features/Callback/CallbackPage.tsx

This file was deleted.

3 changes: 0 additions & 3 deletions src/features/Callback/index.ts

This file was deleted.

8 changes: 1 addition & 7 deletions src/features/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import useLoginUrl from '@site/src/hooks/useLoginUrl';
import Footer from '@site/src/components/Footer';
import Translate from '@docusaurus/Translate';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import { useHandleLogin } from '@site/src/hooks/useHandleLogin';

export const Login = () => {
const { getUrl } = useLoginUrl();
Expand All @@ -16,11 +15,6 @@ export const Login = () => {
const handleClick = () => {
window.location.assign(getUrl(currentLocale));
};

const { handleLogin } = useHandleLogin({
onClickLogin: handleClick,
});

return (
<div>
<div className={styles.login} data-testid='login'>
Expand All @@ -32,7 +26,7 @@ export const Login = () => {
</Translate>
</Text>
<div className={styles.action}>
<Button color='primary' onClick={handleLogin}>
<Button color='primary' onClick={handleClick}>
<Translate>Log In</Translate>
</Button>
</div>
Expand Down
6 changes: 0 additions & 6 deletions src/features/dashboard/__tests__/dashboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ mockReactTable.mockImplementation(() => ({
headerGroups: [],
}));

jest.mock('@docusaurus/BrowserOnly', () => ({
__esModule: true,
default: ({ children }: { children: () => JSX.Element }) => children(),
}));

describe('AppManager', () => {
it('shows the login screen', () => {
mockUseAuthContext.mockImplementation(() => ({
Expand All @@ -64,7 +59,6 @@ describe('AppManager', () => {
const login = screen.getByText(
/Log in to your Deriv account to get the API token and start using our API./i,
);

expect(login).toBeInTheDocument();
});

Expand Down
3 changes: 1 addition & 2 deletions src/features/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import useAuthContext from '@site/src/hooks/useAuthContext';
import useAppManager from '@site/src/hooks/useAppManager';
import ManageDashboard from './manage-dashboard';
import { Login } from '../Login/Login';
import BrowserOnly from '@docusaurus/BrowserOnly';

const Dashboard = () => {
const { is_logged_in } = useAuthContext();
Expand All @@ -17,7 +16,7 @@ const Dashboard = () => {
}, [setIsDashboard]);

if (is_logged_in) return <ManageDashboard />;
return <BrowserOnly>{() => <Login />}</BrowserOnly>;
return <Login />;
};

export default Dashboard;
39 changes: 0 additions & 39 deletions src/hooks/useHandleLogin/index.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions src/hooks/useLogout/__tests__/useLogout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ mockUseAuthContext.mockImplementation(() => ({
updateCurrentLoginAccount: mockUpdateCurrentLoginAccount,
}));

jest.mock('@deriv-com/auth-client', () => ({
OAuth2Logout: jest.fn((WSLogoutAndRedirect) => {
const mockIframe = document.createElement('iframe');
mockIframe.id = 'logout-iframe';
document.body.appendChild(mockIframe);

setTimeout(() => {
const event = new MessageEvent('message', { data: 'logout_complete' });
window.dispatchEvent(event);
}, 100);

WSLogoutAndRedirect();
}),
}));

const logout_response = {
logout: 1,
req_id: 1,
Expand All @@ -59,7 +44,6 @@ describe('Login', () => {
});

await expect(wsServer).toReceiveMessage(logout_response);

wsServer.send({ logout: 1 });

expect(mockUpdateLoginAccounts).toBeCalledTimes(1);
Expand Down
13 changes: 8 additions & 5 deletions src/hooks/useLogout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import apiManager from '@site/src/configs/websocket';
import { useCallback } from 'react';
import useAuthContext from '../useAuthContext';
import { OAuth2Logout } from '@deriv-com/auth-client';
import useGrowthbookGetFeatureValue from '../useGrowthbookGetFeatureValue';
import { useOAuth2, TOAuth2EnabledAppList } from '@deriv-com/auth-client';

const useLogout = () => {
const { updateLoginAccounts, updateCurrentLoginAccount } = useAuthContext();
const [OAuth2EnabledApps, OAuth2EnabledAppsInitialised] =
useGrowthbookGetFeatureValue<TOAuth2EnabledAppList>({
featureFlag: 'hydra_be',
});

// we clean up everything related to the user here, for now it's just user's account
// later on we should clear user tokens as well
Expand All @@ -18,11 +23,9 @@ const useLogout = () => {
});
}, [updateCurrentLoginAccount, updateLoginAccounts]);

const handleLogout = () => {
OAuth2Logout(logout);
};
const { OAuth2Logout } = useOAuth2({ OAuth2EnabledApps, OAuth2EnabledAppsInitialised }, logout);

return { logout: handleLogout };
return { logout: OAuth2Logout };
};

export default useLogout;
5 changes: 2 additions & 3 deletions src/pages/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import useAuthParams from '../hooks/useAuthParams';
import { useEffect } from 'react';
import { Redirect, useLocation } from '@docusaurus/router';
import useAuthContext from '../hooks/useAuthContext';
import BrowserOnly from '@docusaurus/BrowserOnly';

export default function Auth(): JSX.Element {
const { search } = useLocation(); // to get the search params
Expand All @@ -20,7 +19,7 @@ export default function Auth(): JSX.Element {
useEffect(() => {
if (is_logged_in) {
const params = new URLSearchParams(search);
const redirect_route = params.get('route')?.replace(/%2F/g, '/') || '/';
const redirect_route = params.get('route')?.replace(/%2F/g, '/') || '/';
setRedirectRoute(redirect_route);
}
}, [is_logged_in, search]);
Expand All @@ -32,7 +31,7 @@ export default function Auth(): JSX.Element {
return (
<Layout title='Auth' description='Deriv API documentation'>
<main>
<BrowserOnly>{() => <Login />}</BrowserOnly>
<Login />
</main>
</Layout>
);
Expand Down
Loading

0 comments on commit b3930fc

Please sign in to comment.