|
| 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; |
0 commit comments