-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39fc911
commit fae3dba
Showing
4 changed files
with
95 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React from 'react'; | ||
import ChunkLoader from '@/components/loader/chunk-loader'; | ||
import { generateDerivApiInstance } from '@/external/bot-skeleton/services/api/appId'; | ||
import { localize } from '@deriv-com/translations'; | ||
import { URLUtils } from '@deriv-com/utils'; | ||
import App from './App'; | ||
|
||
const setLocalStorageToken = async (loginInfo: URLUtils.LoginInfo[], paramsToDelete: string[]) => { | ||
if (loginInfo.length) { | ||
try { | ||
const defaultActiveAccount = URLUtils.getDefaultActiveAccount(loginInfo); | ||
if (!defaultActiveAccount) return; | ||
|
||
const accountsList: Record<string, string> = {}; | ||
const clientAccounts: Record<string, { loginid: string; token: string; currency: string }> = {}; | ||
|
||
loginInfo.forEach((account: { loginid: string; token: string; currency: string }) => { | ||
accountsList[account.loginid] = account.token; | ||
clientAccounts[account.loginid] = account; | ||
}); | ||
|
||
localStorage.setItem('accountsList', JSON.stringify(accountsList)); | ||
localStorage.setItem('clientAccounts', JSON.stringify(clientAccounts)); | ||
URLUtils.filterSearchParams(paramsToDelete); | ||
const api = await generateDerivApiInstance(); | ||
|
||
if (api) { | ||
const { authorize, error } = await api.authorize(loginInfo[0].token); | ||
api.disconnect(); | ||
if (!error) { | ||
const firstId = authorize?.account_list[0]?.loginid; | ||
const filteredTokens = loginInfo.filter(token => token.loginid === firstId); | ||
if (filteredTokens.length) { | ||
localStorage.setItem('authToken', filteredTokens[0].token); | ||
localStorage.setItem('active_loginid', filteredTokens[0].loginid); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
localStorage.setItem('authToken', loginInfo[0].token); | ||
localStorage.setItem('active_loginid', loginInfo[0].loginid); | ||
} catch (error) { | ||
console.error('Error setting up login info:', error); | ||
} | ||
} | ||
}; | ||
|
||
export const AuthWrapper = () => { | ||
const [isAuthComplete, setIsAuthComplete] = React.useState(false); | ||
const { loginInfo, paramsToDelete } = URLUtils.getLoginInfoFromURL(); | ||
|
||
React.useEffect(() => { | ||
const initializeAuth = async () => { | ||
await setLocalStorageToken(loginInfo, paramsToDelete); | ||
URLUtils.filterSearchParams(['lang']); | ||
setIsAuthComplete(true); | ||
}; | ||
|
||
initializeAuth(); | ||
}, [loginInfo, paramsToDelete]); | ||
|
||
if (!isAuthComplete) { | ||
return <ChunkLoader message={localize('Initializing...')} />; | ||
} | ||
|
||
return <App />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import ReactDOM from 'react-dom/client'; | ||
import App from './app/App'; | ||
import { AuthWrapper } from './app/AuthWrapper'; | ||
import { AnalyticsInitializer } from './utils/analytics'; | ||
import './styles/index.scss'; | ||
|
||
AnalyticsInitializer(); | ||
|
||
ReactDOM.createRoot(document.getElementById('root')!).render(<App />); | ||
ReactDOM.createRoot(document.getElementById('root')!).render(<AuthWrapper />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters