Skip to content

Commit

Permalink
Merge pull request #285 from rupato-deriv/Rupato/Fix-silent-login-usi…
Browse files Browse the repository at this point in the history
…ng-query-param

fix: silent login
  • Loading branch information
rupato-deriv authored Feb 19, 2025
2 parents feaa7f7 + d6100bb commit 7398989
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
20 changes: 10 additions & 10 deletions src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ function App() {
};
}, []);

// const updateAccountParamInURL = (account_data: TAuthData['account_list'][number], fallback_currency = '') => {
// const search_params = new URLSearchParams(window.location.search);
// const account_param = account_data.loginid.startsWith('VR')
// ? 'demo'
// : account_data.currency || fallback_currency;
// search_params.set('account', account_param);
// //window.history.pushState({}, '', `${window.location.pathname}?${search_params.toString()}`);
// };
const updateAccountParamInURL = (account_data: TAuthData['account_list'][number], fallback_currency = '') => {
const search_params = new URLSearchParams(window.location.search);
const account_param = account_data.loginid.startsWith('VR')
? 'demo'
: account_data.currency || fallback_currency;
search_params.set('account', account_param);
window.history.pushState({}, '', `${window.location.pathname}?${search_params.toString()}`);
};

React.useEffect(() => {
const accounts_list = localStorage.getItem('accountsList');
Expand All @@ -85,7 +85,7 @@ function App() {
);
if (!selected_account) return;
const [/* eslint-disable-line @typescript-eslint/no-unused-vars */ _, account] = selected_account;
//updateAccountParamInURL(account);
updateAccountParamInURL(account);
} catch (e) {
console.warn('Error', e); // eslint-disable-line no-console
}
Expand Down Expand Up @@ -142,7 +142,7 @@ function App() {
);
if (!selected_account) return;
const [_, account] = selected_account; // eslint-disable-line @typescript-eslint/no-unused-vars
//updateAccountParamInURL(account, 'USD');
updateAccountParamInURL(account, 'USD');
}
} catch (e) {
console.warn('Error', e); // eslint-disable-line no-console
Expand Down
49 changes: 38 additions & 11 deletions src/components/layout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import clsx from 'clsx';
import Cookies from 'js-cookie';
import { Outlet } from 'react-router-dom';
import { api_base } from '@/external/bot-skeleton';
import { useOauth2 } from '@/hooks/auth/useOauth2';
import { requestOidcAuthentication } from '@deriv-com/auth-client';
import { useDevice } from '@deriv-com/ui';
import { crypto_currencies_display_order, fiat_currencies_display_order } from '../shared';
import Footer from './footer';
import AppHeader from './header';
import Body from './main-body';
Expand All @@ -21,24 +23,49 @@ const Layout = () => {
const checkClientAccount = JSON.parse(localStorage.getItem('clientAccounts') ?? '{}');
const getQueryParams = new URLSearchParams(window.location.search);
const currency = getQueryParams.get('account') ?? '';
const clientAccounts = JSON.parse(localStorage.getItem('accountsList') ?? '{}');
const isClientAccountsPopulated = Object.keys(clientAccounts).length > 0;

const accountsList = JSON.parse(localStorage.getItem('accountsList') ?? '{}');
const isClientAccountsPopulated = Object.keys(accountsList).length > 0;
const ifClientAccountHasCurrency =
Object.values(checkClientAccount).some(account => account.currency === currency) ||
currency === 'demo' ||
currency === '';
const [clientHasCurrency, setClientHasCurrency] = useState(ifClientAccountHasCurrency);

const validCurrencies = [...fiat_currencies_display_order, ...crypto_currencies_display_order];
const query_currency = (getQueryParams.get('account') ?? '')?.toUpperCase();
const isCurrencyValid = validCurrencies.includes(query_currency);
const api_accounts = [];
let subscription: { unsubscribe: () => void };

const validateApiAccounts = ({ data }: any) => {

Check warning on line 40 in src/components/layout/index.tsx

View workflow job for this annotation

GitHub Actions / build_to_cloudflare_pages

Unexpected any. Specify a different type
if (data.msg_type === 'authorize') {
api_accounts.push(data.authorize.account_list || []);
api_accounts?.flat().map(data => {
Object.values(checkClientAccount).map(key => {
if (data.currency !== key.currency) {
console.log('setClientHasCurrency');
setClientHasCurrency(false);
}
});
});

console.log('clientAccounts', {
currency,
checkClientAccount,
ifClientAccountHasCurrency,
});
if (subscription) {
subscription?.unsubscribe();
}
}
};

useEffect(() => {
if (isCurrencyValid && api_base.api) {
// Subscribe to the onMessage event
subscription = api_base.api.onMessage().subscribe(validateApiAccounts);

Check warning on line 61 in src/components/layout/index.tsx

View workflow job for this annotation

GitHub Actions / build_to_cloudflare_pages

Assignments to the 'subscription' variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect
}
}, []);

useEffect(() => {
if (
(isLoggedInCookie && !isClientAccountsPopulated && isOAuth2Enabled && !isEndpointPage && !isCallbackPage) ||
!ifClientAccountHasCurrency
!clientHasCurrency
) {
console.log('requestOidcAuthentication');
requestOidcAuthentication({
Expand All @@ -51,7 +78,7 @@ const Layout = () => {
isOAuth2Enabled,
isEndpointPage,
isCallbackPage,
ifClientAccountHasCurrency,
clientHasCurrency,
]);

return (
Expand Down
4 changes: 2 additions & 2 deletions src/components/shared/utils/currency/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export type TCurrenciesConfig = {

let currencies_config: TCurrenciesConfig = {};

const fiat_currencies_display_order = ['USD', 'EUR', 'GBP', 'AUD'];
const crypto_currencies_display_order = [
export const fiat_currencies_display_order = ['USD', 'EUR', 'GBP', 'AUD'];
export const crypto_currencies_display_order = [
'TUSDT',
'BTC',
'ETH',
Expand Down

0 comments on commit 7398989

Please sign in to comment.