diff --git a/src/App.tsx b/src/App.tsx index 83743d4..8370100 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,7 +7,7 @@ import { RootPathContext } from '@asb/context' import { UserOutlet } from '@components/outlets' import LayoutPortal from '@components/LayoutPortal' import SuspenseFallback from '@components/SuspenseFallback' -import { getUserToken, NoAccessComponent } from '@utils' +import { getUser, NoAccessComponent } from '@utils' import { OpenAPI } from '@api' import '@styles/include/antd_theme.less' @@ -23,7 +23,7 @@ const Cluster = lazy(() => import('@pages/Cluster')) const Well = lazy(() => import('@asb/pages/Well')) //OpenAPI.BASE = 'http://localhost:3000' -OpenAPI.TOKEN = async () => getUserToken() ?? '' +OpenAPI.TOKEN = async () => getUser().token ?? '' OpenAPI.HEADERS = { 'Content-Type': 'application/json' } export const App = memo(() => ( diff --git a/src/components/ChangePassword.tsx b/src/components/ChangePassword.tsx index dc4c5bd..a97eed8 100755 --- a/src/components/ChangePassword.tsx +++ b/src/components/ChangePassword.tsx @@ -3,8 +3,8 @@ import { Rule } from 'antd/lib/form' import { Form, Input, Modal, FormProps } from 'antd' import { AuthService, UserDto } from '@api' -import { getUserId, getUserLogin } from '@utils' import { passwordRules, createPasswordRules } from '@utils/validationRules' +import { getUser } from '@utils' import LoaderPortal from './LoaderPortal' import { invokeWebApiWrapperAsync } from './factory' @@ -31,7 +31,7 @@ export const ChangePassword = memo(({ user, visible, onCanc const [showLoader, setShowLoader] = useState(false) const [isDisabled, setIsDisabled] = useState(true) - const userData = useMemo(() => user ?? { id: getUserId(), login: getUserLogin() } as UserDto, [user]) + const userData = useMemo(() => user ?? getUser(), [user]) const [form] = Form.useForm() diff --git a/src/components/factory.tsx b/src/components/factory.tsx index 4ba138c..9565146 100644 --- a/src/components/factory.tsx +++ b/src/components/factory.tsx @@ -3,8 +3,7 @@ import { ArgsProps } from 'antd/lib/notification' import { Dispatch, ReactNode, SetStateAction } from 'react' import { WellView } from '@components/views' -import { getUserToken } from '@utils' -import { FunctionalValue, getFunctionalValue, isDev } from '@utils' +import { FunctionalValue, getFunctionalValue, getUser, isDev } from '@utils' import { ApiError, FileInfoDto, WellDto } from '@api' export type NotifyType = 'error' | 'warning' | 'info' @@ -97,7 +96,7 @@ export const invokeWebApiWrapperAsync = async ( export const download = async (url: string, fileName?: string) => { const response = await fetch(url, { headers: { - Authorization: `Bearer ${getUserToken()}` + Authorization: `Bearer ${getUser().token}` }, method: 'Get' }) @@ -125,7 +124,7 @@ export const download = async (url: string, fileName?: string) => { export const upload = async (url: string, formData: FormData) => { const response = await fetch(url, { headers: { - Authorization: `Bearer ${getUserToken()}` + Authorization: `Bearer ${getUser().token}` }, method: 'Post', body: formData, diff --git a/src/services/signalr/index.ts b/src/services/signalr/index.ts index 5a8ed93..8c2d684 100755 --- a/src/services/signalr/index.ts +++ b/src/services/signalr/index.ts @@ -1,6 +1,6 @@ import { HubConnection, HubConnectionBuilder, HubConnectionState } from '@microsoft/signalr' -import { getUserToken, isDev } from '@utils' +import { getUser, isDev } from '@utils' import { makeTaskQueue } from '@utils/queue' const queue = makeTaskQueue() @@ -8,8 +8,8 @@ const queue = makeTaskQueue() // SignalR js api: //https://docs.microsoft.com/ru-ru/javascript/api/@aspnet/signalr/?view=signalr-js-latest -const ConnectionOptions = { - accessTokenFactory: () => getUserToken() ?? '', +const ConnectionOptions = { + accessTokenFactory: () => getUser().token ?? '', transport: 1, } diff --git a/src/utils/functions/permissions.tsx b/src/utils/functions/permissions.tsx index baa749d..6375502 100644 --- a/src/utils/functions/permissions.tsx +++ b/src/utils/functions/permissions.tsx @@ -1,8 +1,7 @@ import { memo, NamedExoticComponent, ReactElement, ReactNode, useMemo } from 'react' import { Navigate, useParams } from 'react-router-dom' -import { getUserLogin, getUserPermissions } from '@utils' -import { isDev } from '@utils' +import { isDev, getUser, getUserPermissions } from '@utils' import AccessDenied from '@pages/public/AccessDenied' @@ -23,7 +22,7 @@ export const getPermissions = (...values: PermissionRequest[]) => { const [service, type] = (key[0].toLowerCase() + key.slice(1)).split('.') // toCamelCase if (!isRequestType(type)) return permissions[service] = permissions[service] ?? {} - permissions[service][type] = hasPermission(key) || (isDev() && getUserLogin() === 'dev') + permissions[service][type] = hasPermission(key) || (isDev() && getUser().login === 'dev') }) return permissions } @@ -100,7 +99,7 @@ export type PrivateComponent

= NamedExoticComponent

& Priva getKey: () => string } -export const NoAccessComponent = memo(() => getUserLogin() ? ( +export const NoAccessComponent = memo(() => getUser().login ? ( // ) : ( diff --git a/src/utils/functions/storage.ts b/src/utils/functions/storage.ts index a75a33f..df1bcfc 100644 --- a/src/utils/functions/storage.ts +++ b/src/utils/functions/storage.ts @@ -10,7 +10,6 @@ export enum StorageNames { token = 'token', login = 'login', permissions = 'permissions', - roles = 'roles', user = 'user', tableSettings = 'tableSettings', dashboardNNB = 'dashboardNNB', @@ -43,7 +42,15 @@ export const setJSON = (name: StorageNames, data: T | null): boolean => { return false } -export const getUser = (): UserTokenDto | null => getJSON(StorageNames.user) +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 getUserPermissions = (): Permission[] | null => { let permissions = getUser()?.permissions?.map((perm) => perm.name as string) @@ -51,19 +58,12 @@ export const getUserPermissions = (): Permission[] | null => { permissions = localStorage.getItem(StorageNames.permissions)?.split(',') return permissions || null } -export const getUserId = () => Number(localStorage.getItem(StorageNames.userId)) || null -export const getUserLogin = () => localStorage.getItem(StorageNames.login) -export const getUserToken = () => localStorage.getItem(StorageNames.token) export const setUser = (user: UserTokenDto) => { OpenAPI.TOKEN = user.token ?? undefined localStorage.setItem(StorageNames.user, JSON.stringify(user)) - localStorage.setItem(StorageNames.userId, String(user.id)) localStorage.setItem(StorageNames.token, String(user.token)) - localStorage.setItem(StorageNames.login, String(user.login)) - if (user.permissions) - localStorage.setItem(StorageNames.permissions, user.permissions.map((permission) => permission.name).join(',')) } export const removeUser = () => {