diff --git a/src/context/auth-context.tsx b/src/context/auth-context.tsx index e2ce7b1..04dce7b 100644 --- a/src/context/auth-context.tsx +++ b/src/context/auth-context.tsx @@ -25,15 +25,20 @@ export const AuthDataProvider = ({ children }: AuthDataProviderProps) => { const { data, mutate, isSuccess, isPending } = useAuthorize(); + const accountsList: Record = JSON.parse(Cookies.get('accountsList') ?? '{}'); + + const isAuthorized = useMemo( + () => !!activeLoginid || !!Object.keys(accountsList).length, + [activeLoginid, accountsList] + ); + const authorizeAccount = useCallback((token?: string) => { if (token) mutate({ authorize: token }); }, []); const switchAccount = useCallback( (loginId: string) => { - const tradingAccounts: Record = JSON.parse(Cookies.get('tradingAccounts') ?? '{}'); - - const token = tradingAccounts[loginId]; + const token = accountsList[loginId]; if (!token) return; authorizeAccount(token); @@ -55,13 +60,13 @@ export const AuthDataProvider = ({ children }: AuthDataProviderProps) => { if (!defaultActiveAccount) return; setActiveLoginid(loginInfo[0].loginid); - const tradingAccounts: Record = {}; + const accountsList: Record = {}; loginInfo.forEach(account => { - tradingAccounts[account.loginid] = account.token; + accountsList[account.loginid] = account.token; }); - Cookies.set('tradingAccounts', JSON.stringify(tradingAccounts)); + Cookies.set('accountsList', JSON.stringify(accountsList)); URLUtils.filterSearchParams(paramsToDelete); authorizeAccount(loginInfo[0].token); @@ -75,11 +80,9 @@ export const AuthDataProvider = ({ children }: AuthDataProviderProps) => { }, []); const appendAccountCookie = useCallback((loginid: string, token: string) => { - const tradingAccounts: Record = JSON.parse(Cookies.get('tradingAccounts') ?? '{}'); - - tradingAccounts[loginid] = token; + accountsList[loginid] = token; - Cookies.set('tradingAccounts', JSON.stringify(tradingAccounts)); + Cookies.set('accountsList', JSON.stringify(accountsList)); switchAccount(loginid); }, []); @@ -93,11 +96,11 @@ export const AuthDataProvider = ({ children }: AuthDataProviderProps) => { const value = useMemo( () => ({ activeLoginid, - isAuthorized: !!activeLoginid && isSuccess, isAuthorizing: isPending, switchAccount, appendAccountCookie, logout, + isAuthorized, }), [activeLoginid, isSuccess] );