forked from ddrilling/asb_cloud_front
Улучшена работа с хранилищем
This commit is contained in:
parent
e852ede73c
commit
121cb83d83
@ -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(() => (
|
||||
|
@ -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<ChangePasswordProps>(({ user, visible, onCanc
|
||||
const [showLoader, setShowLoader] = useState<boolean>(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()
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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<void>()
|
||||
@ -8,8 +8,8 @@ const queue = makeTaskQueue<void>()
|
||||
// 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,
|
||||
}
|
||||
|
||||
|
@ -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<P extends object> = NamedExoticComponent<P> & Priva
|
||||
getKey: () => string
|
||||
}
|
||||
|
||||
export const NoAccessComponent = memo(() => getUserLogin() ? (
|
||||
export const NoAccessComponent = memo(() => getUser().login ? (
|
||||
<AccessDenied /> // <Navigate to={'/access_denied'} replace />
|
||||
) : (
|
||||
<Navigate to={'/login'} replace />
|
||||
|
@ -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 = <T,>(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 = () => {
|
||||
|
Loading…
Reference in New Issue
Block a user