Skip to content

Commit 8fd853a

Browse files
Streamline OS Redirect logic and enhance cookie management for accounts redirection (#17877)
* feat: add OSRedirect component and update routes config * chore: wait for logged in state before redirection * chore: add OSRedirect component and update routes configuration * chore: restore unchanged files * refactor: streamline OSRedirect logic and enhance cookie management * refactor: update redirect comment for clarity on logged-in user behavior --------- Co-authored-by: Habib Deriv <[email protected]>
1 parent 7758ad2 commit 8fd853a

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

packages/core/src/App/Constants/routes-config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Redirect from 'App/Containers/Redirect';
99
import RootComponent from 'App/Containers/RootComponent';
1010
import Endpoint from 'Modules/Endpoint';
1111

12+
import OSRedirect from '../Containers/OSRedirect';
1213
import CallbackPage from '../../Modules/Callback/CallbackPage.tsx';
1314

1415
const CFDCompareAccounts = React.lazy(
@@ -381,6 +382,7 @@ const lazyLoadComplaintsPolicy = makeLazyLoader(
381382
const initRoutesConfig = () => [
382383
{ path: routes.index, component: RouterRedirect, getTitle: () => '', to: routes.traders_hub },
383384
{ path: routes.endpoint, component: Endpoint, getTitle: () => 'Endpoint' }, // doesn't need localization as it's for internal use
385+
{ path: routes.os_redirect, component: OSRedirect, getTitle: () => localize('Redirect') },
384386
{ path: routes.redirect, component: Redirect, getTitle: () => localize('Redirect') },
385387
{ path: routes.callback_page, component: CallbackPage, getTitle: () => 'Callback' },
386388
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import OSRedirect from './os-redirect';
2+
3+
export default OSRedirect;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { useEffect } from 'react';
2+
import { useHistory } from 'react-router-dom';
3+
import Cookies from 'js-cookie';
4+
5+
import { getDomainName, routes } from '@deriv/shared';
6+
import { useStore } from '@deriv/stores';
7+
import { Loader } from '@deriv-com/ui';
8+
9+
const OSRedirect = () => {
10+
const {
11+
client: { is_logged_in },
12+
} = useStore();
13+
const history = useHistory();
14+
15+
// TODO: remove this after oauth2 migration
16+
// get data from cookies and populate local storage for clients
17+
// to be logged in coming from OS subdomains
18+
const client_accounts = Cookies.get('client.accounts');
19+
const active_loginid = Cookies.get('active_loginid');
20+
const active_wallet_loginid = Cookies.get('active_wallet_loginid');
21+
22+
if (client_accounts && active_loginid) {
23+
localStorage.setItem('client.accounts', client_accounts);
24+
localStorage.setItem('active_loginid', active_loginid);
25+
active_wallet_loginid && localStorage.setItem('active_wallet_loginid', active_wallet_loginid);
26+
27+
// remove cookies after populating local storage
28+
['client.accounts', 'active_loginid', 'active_wallet_loginid'].forEach(cookie => {
29+
Cookies.remove(cookie, { domain: getDomainName(), secure: true });
30+
});
31+
32+
window.location.reload();
33+
}
34+
35+
useEffect(() => {
36+
const url_query_string = window.location.search;
37+
const params = new URLSearchParams(url_query_string);
38+
params.delete('redirect_to');
39+
40+
const routes_list = [
41+
{ pattern: /proof-of-address/i, route: routes.proof_of_address },
42+
{ pattern: /proof-of-identity/i, route: routes.proof_of_identity },
43+
{ pattern: /personal-details/i, route: routes.personal_details },
44+
{ pattern: /financial-assessment/i, route: routes.financial_assessment },
45+
{ pattern: /trading-assessment/i, route: routes.trading_assessment },
46+
{ pattern: /accumulator/i, route: routes.trade, type: 'accumulator' },
47+
{ pattern: /turbos/i, route: routes.trade, type: 'turboslong' },
48+
{ pattern: /vanilla/i, route: routes.trade, type: 'vanillalongcall' },
49+
{ pattern: /multiplier/i, route: routes.trade, type: 'multiplier' },
50+
];
51+
const route = routes_list.find(({ pattern }) => pattern.test(url_query_string));
52+
route?.type && params.set('trade_type', route.type);
53+
/**
54+
* Redirect to route if user is logged in
55+
* Need to wait logged in state to be updated before redirecting
56+
*/
57+
if (is_logged_in && route) {
58+
return history.push({
59+
pathname: route?.route,
60+
// @ts-expect-error need to update react-router-dom types
61+
search: params.toString(),
62+
});
63+
}
64+
}, [history, is_logged_in]);
65+
66+
return <Loader isFullScreen />;
67+
};
68+
69+
export default OSRedirect;

packages/shared/src/utils/routes/routes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ export const routes = {
9494
wallets_compare_accounts: '/compare-accounts',
9595
wallets_on_ramp: '/wallet/on-ramp',
9696
wallets_reset_balance: '/wallet/reset-balance',
97+
98+
// Outsystems
99+
os_redirect: '/os-redirect',
97100
};
98101

99102
export const DISABLE_LANDSCAPE_BLOCKER_ROUTES = [

0 commit comments

Comments
 (0)