diff --git a/src/App.tsx b/src/App.tsx index 8370100..59c64fb 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -23,7 +23,8 @@ const Cluster = lazy(() => import('@pages/Cluster')) const Well = lazy(() => import('@asb/pages/Well')) //OpenAPI.BASE = 'http://localhost:3000' -OpenAPI.TOKEN = async () => getUser().token ?? '' +// TODO: Удалить взятие из 'token' в следующем релизе, вставлено для совместимости +OpenAPI.TOKEN = async () => getUser().token || localStorage.getItem('token') || '' OpenAPI.HEADERS = { 'Content-Type': 'application/json' } export const App = memo(() => ( diff --git a/src/components/ChangePassword.tsx b/src/components/ChangePassword.tsx index a97eed8..0ab8890 100755 --- a/src/components/ChangePassword.tsx +++ b/src/components/ChangePassword.tsx @@ -2,9 +2,9 @@ import { memo, useCallback, useMemo, useState } from 'react' import { Rule } from 'antd/lib/form' import { Form, Input, Modal, FormProps } from 'antd' +import { useUser } from '@asb/context' import { AuthService, UserDto } from '@api' import { passwordRules, createPasswordRules } from '@utils/validationRules' -import { getUser } from '@utils' import LoaderPortal from './LoaderPortal' import { invokeWebApiWrapperAsync } from './factory' @@ -31,7 +31,8 @@ export const ChangePassword = memo(({ user, visible, onCanc const [showLoader, setShowLoader] = useState(false) const [isDisabled, setIsDisabled] = useState(true) - const userData = useMemo(() => user ?? getUser(), [user]) + const userContext = useUser() + const userData = useMemo(() => user ?? userContext, [user]) const [form] = Form.useForm() diff --git a/src/components/outlets/UserOutlet.tsx b/src/components/outlets/UserOutlet.tsx index 49a2859..236eef7 100644 --- a/src/components/outlets/UserOutlet.tsx +++ b/src/components/outlets/UserOutlet.tsx @@ -1,11 +1,24 @@ -import { memo, useMemo } from 'react' +import { memo, useEffect, useState } from 'react' import { Outlet } from 'react-router-dom' import { UserContext } from '@asb/context' -import { getUser } from '@utils' +import { invokeWebApiWrapperAsync } from '@components/factory' +import { getUser, setUser as setStorageUser } from '@utils' +import { AuthService, UserTokenDto } from '@api' export const UserOutlet = memo(() => { - const user = useMemo(() => getUser() ?? {}, []) + const [user, setUser] = useState({}) + + useEffect(() => { + invokeWebApiWrapperAsync(async () => { + let user = getUser() + if (!user.id) { + user = await AuthService.refresh() + setStorageUser(user) + } + setUser(user) + }) + }, []) return ( diff --git a/src/utils/functions/storage.ts b/src/utils/functions/storage.ts index df1bcfc..85772b7 100644 --- a/src/utils/functions/storage.ts +++ b/src/utils/functions/storage.ts @@ -42,15 +42,7 @@ export const setJSON = (name: StorageNames, data: T | null): boolean => { return false } -export const getUser = (): UserTokenDto => { - const stored = getJSON(StorageNames.user) - if (stored) return stored - return { // TODO: Удалить в следующем релизе, вставлено для совместимости - id: Number(localStorage.getItem(StorageNames.userId)) ?? undefined, - login: localStorage.getItem(StorageNames.login) || undefined, - token: localStorage.getItem(StorageNames.token) || undefined, - } -} +export const getUser = (): UserTokenDto => getJSON(StorageNames.user) || {} export const getUserPermissions = (): Permission[] | null => { let permissions = getUser()?.permissions?.map((perm) => perm.name as string) @@ -62,8 +54,6 @@ export const getUserPermissions = (): Permission[] | null => { export const setUser = (user: UserTokenDto) => { OpenAPI.TOKEN = user.token ?? undefined localStorage.setItem(StorageNames.user, JSON.stringify(user)) - - localStorage.setItem(StorageNames.token, String(user.token)) } export const removeUser = () => {