From 44543fec31da0b8ce808d2d6c5685ae77a549bea Mon Sep 17 00:00:00 2001 From: goodmice Date: Mon, 24 Oct 2022 12:18:38 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D1=8F,=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=BE=D0=BD=D0=B8=20?= =?UTF-8?q?=D0=BE=D1=82=D1=81=D1=83=D1=82=D1=81=D1=82=D0=B2=D1=83=D1=8E?= =?UTF-8?q?=D1=82=20=D0=B2=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8?= =?UTF-8?q?=D1=89=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.tsx | 3 ++- src/components/ChangePassword.tsx | 5 +++-- src/components/outlets/UserOutlet.tsx | 19 ++++++++++++++++--- src/utils/functions/storage.ts | 12 +----------- 4 files changed, 22 insertions(+), 17 deletions(-) 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 = () => {