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 { UserOutlet } from '@components/outlets'
|
||||||
import LayoutPortal from '@components/LayoutPortal'
|
import LayoutPortal from '@components/LayoutPortal'
|
||||||
import SuspenseFallback from '@components/SuspenseFallback'
|
import SuspenseFallback from '@components/SuspenseFallback'
|
||||||
import { getUserToken, NoAccessComponent } from '@utils'
|
import { getUser, NoAccessComponent } from '@utils'
|
||||||
import { OpenAPI } from '@api'
|
import { OpenAPI } from '@api'
|
||||||
|
|
||||||
import '@styles/include/antd_theme.less'
|
import '@styles/include/antd_theme.less'
|
||||||
@ -23,7 +23,7 @@ const Cluster = lazy(() => import('@pages/Cluster'))
|
|||||||
const Well = lazy(() => import('@asb/pages/Well'))
|
const Well = lazy(() => import('@asb/pages/Well'))
|
||||||
|
|
||||||
//OpenAPI.BASE = 'http://localhost:3000'
|
//OpenAPI.BASE = 'http://localhost:3000'
|
||||||
OpenAPI.TOKEN = async () => getUserToken() ?? ''
|
OpenAPI.TOKEN = async () => getUser().token ?? ''
|
||||||
OpenAPI.HEADERS = { 'Content-Type': 'application/json' }
|
OpenAPI.HEADERS = { 'Content-Type': 'application/json' }
|
||||||
|
|
||||||
export const App = memo(() => (
|
export const App = memo(() => (
|
||||||
|
@ -3,8 +3,8 @@ import { Rule } from 'antd/lib/form'
|
|||||||
import { Form, Input, Modal, FormProps } from 'antd'
|
import { Form, Input, Modal, FormProps } from 'antd'
|
||||||
|
|
||||||
import { AuthService, UserDto } from '@api'
|
import { AuthService, UserDto } from '@api'
|
||||||
import { getUserId, getUserLogin } from '@utils'
|
|
||||||
import { passwordRules, createPasswordRules } from '@utils/validationRules'
|
import { passwordRules, createPasswordRules } from '@utils/validationRules'
|
||||||
|
import { getUser } from '@utils'
|
||||||
|
|
||||||
import LoaderPortal from './LoaderPortal'
|
import LoaderPortal from './LoaderPortal'
|
||||||
import { invokeWebApiWrapperAsync } from './factory'
|
import { invokeWebApiWrapperAsync } from './factory'
|
||||||
@ -31,7 +31,7 @@ export const ChangePassword = memo<ChangePasswordProps>(({ user, visible, onCanc
|
|||||||
const [showLoader, setShowLoader] = useState<boolean>(false)
|
const [showLoader, setShowLoader] = useState<boolean>(false)
|
||||||
const [isDisabled, setIsDisabled] = useState(true)
|
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()
|
const [form] = Form.useForm()
|
||||||
|
|
||||||
|
@ -3,8 +3,7 @@ import { ArgsProps } from 'antd/lib/notification'
|
|||||||
import { Dispatch, ReactNode, SetStateAction } from 'react'
|
import { Dispatch, ReactNode, SetStateAction } from 'react'
|
||||||
|
|
||||||
import { WellView } from '@components/views'
|
import { WellView } from '@components/views'
|
||||||
import { getUserToken } from '@utils'
|
import { FunctionalValue, getFunctionalValue, getUser, isDev } from '@utils'
|
||||||
import { FunctionalValue, getFunctionalValue, isDev } from '@utils'
|
|
||||||
import { ApiError, FileInfoDto, WellDto } from '@api'
|
import { ApiError, FileInfoDto, WellDto } from '@api'
|
||||||
|
|
||||||
export type NotifyType = 'error' | 'warning' | 'info'
|
export type NotifyType = 'error' | 'warning' | 'info'
|
||||||
@ -97,7 +96,7 @@ export const invokeWebApiWrapperAsync = async (
|
|||||||
export const download = async (url: string, fileName?: string) => {
|
export const download = async (url: string, fileName?: string) => {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${getUserToken()}`
|
Authorization: `Bearer ${getUser().token}`
|
||||||
},
|
},
|
||||||
method: 'Get'
|
method: 'Get'
|
||||||
})
|
})
|
||||||
@ -125,7 +124,7 @@ export const download = async (url: string, fileName?: string) => {
|
|||||||
export const upload = async (url: string, formData: FormData) => {
|
export const upload = async (url: string, formData: FormData) => {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${getUserToken()}`
|
Authorization: `Bearer ${getUser().token}`
|
||||||
},
|
},
|
||||||
method: 'Post',
|
method: 'Post',
|
||||||
body: formData,
|
body: formData,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { HubConnection, HubConnectionBuilder, HubConnectionState } from '@microsoft/signalr'
|
import { HubConnection, HubConnectionBuilder, HubConnectionState } from '@microsoft/signalr'
|
||||||
|
|
||||||
import { getUserToken, isDev } from '@utils'
|
import { getUser, isDev } from '@utils'
|
||||||
import { makeTaskQueue } from '@utils/queue'
|
import { makeTaskQueue } from '@utils/queue'
|
||||||
|
|
||||||
const queue = makeTaskQueue<void>()
|
const queue = makeTaskQueue<void>()
|
||||||
@ -8,8 +8,8 @@ const queue = makeTaskQueue<void>()
|
|||||||
// SignalR js api:
|
// SignalR js api:
|
||||||
//https://docs.microsoft.com/ru-ru/javascript/api/@aspnet/signalr/?view=signalr-js-latest
|
//https://docs.microsoft.com/ru-ru/javascript/api/@aspnet/signalr/?view=signalr-js-latest
|
||||||
|
|
||||||
const ConnectionOptions = {
|
const ConnectionOptions = {
|
||||||
accessTokenFactory: () => getUserToken() ?? '',
|
accessTokenFactory: () => getUser().token ?? '',
|
||||||
transport: 1,
|
transport: 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import { memo, NamedExoticComponent, ReactElement, ReactNode, useMemo } from 'react'
|
import { memo, NamedExoticComponent, ReactElement, ReactNode, useMemo } from 'react'
|
||||||
import { Navigate, useParams } from 'react-router-dom'
|
import { Navigate, useParams } from 'react-router-dom'
|
||||||
|
|
||||||
import { getUserLogin, getUserPermissions } from '@utils'
|
import { isDev, getUser, getUserPermissions } from '@utils'
|
||||||
import { isDev } from '@utils'
|
|
||||||
|
|
||||||
import AccessDenied from '@pages/public/AccessDenied'
|
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
|
const [service, type] = (key[0].toLowerCase() + key.slice(1)).split('.') // toCamelCase
|
||||||
if (!isRequestType(type)) return
|
if (!isRequestType(type)) return
|
||||||
permissions[service] = permissions[service] ?? {}
|
permissions[service] = permissions[service] ?? {}
|
||||||
permissions[service][type] = hasPermission(key) || (isDev() && getUserLogin() === 'dev')
|
permissions[service][type] = hasPermission(key) || (isDev() && getUser().login === 'dev')
|
||||||
})
|
})
|
||||||
return permissions
|
return permissions
|
||||||
}
|
}
|
||||||
@ -100,7 +99,7 @@ export type PrivateComponent<P extends object> = NamedExoticComponent<P> & Priva
|
|||||||
getKey: () => string
|
getKey: () => string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const NoAccessComponent = memo(() => getUserLogin() ? (
|
export const NoAccessComponent = memo(() => getUser().login ? (
|
||||||
<AccessDenied /> // <Navigate to={'/access_denied'} replace />
|
<AccessDenied /> // <Navigate to={'/access_denied'} replace />
|
||||||
) : (
|
) : (
|
||||||
<Navigate to={'/login'} replace />
|
<Navigate to={'/login'} replace />
|
||||||
|
@ -10,7 +10,6 @@ export enum StorageNames {
|
|||||||
token = 'token',
|
token = 'token',
|
||||||
login = 'login',
|
login = 'login',
|
||||||
permissions = 'permissions',
|
permissions = 'permissions',
|
||||||
roles = 'roles',
|
|
||||||
user = 'user',
|
user = 'user',
|
||||||
tableSettings = 'tableSettings',
|
tableSettings = 'tableSettings',
|
||||||
dashboardNNB = 'dashboardNNB',
|
dashboardNNB = 'dashboardNNB',
|
||||||
@ -43,7 +42,15 @@ export const setJSON = <T,>(name: StorageNames, data: T | null): boolean => {
|
|||||||
return false
|
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 => {
|
export const getUserPermissions = (): Permission[] | null => {
|
||||||
let permissions = getUser()?.permissions?.map((perm) => perm.name as string)
|
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(',')
|
permissions = localStorage.getItem(StorageNames.permissions)?.split(',')
|
||||||
return permissions || null
|
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) => {
|
export const setUser = (user: UserTokenDto) => {
|
||||||
OpenAPI.TOKEN = user.token ?? undefined
|
OpenAPI.TOKEN = user.token ?? undefined
|
||||||
localStorage.setItem(StorageNames.user, JSON.stringify(user))
|
localStorage.setItem(StorageNames.user, JSON.stringify(user))
|
||||||
|
|
||||||
localStorage.setItem(StorageNames.userId, String(user.id))
|
|
||||||
localStorage.setItem(StorageNames.token, String(user.token))
|
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 = () => {
|
export const removeUser = () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user