Добавлен перенос на страницу входа при попытке открыть страницу без авторизации

This commit is contained in:
Александр Сироткин 2022-02-07 17:36:19 +05:00
parent e3e5a1bf4e
commit 98da6eb847
2 changed files with 4 additions and 2 deletions

View File

@ -1,6 +1,7 @@
import { memo } from 'react'
import { Redirect, Route, RouteProps } from 'react-router-dom'
import { getUserId } from '@utils/storage'
import { isURLAvailable } from '@utils/permissions'
export type PrivateDefaultRouteProps = RouteProps & {
@ -10,7 +11,7 @@ export type PrivateDefaultRouteProps = RouteProps & {
export const PrivateDefaultRoute = memo<PrivateDefaultRouteProps>(({ elseRedirect, urls, ...other }) => (
<Route {...other} path={'/'}>
<Redirect to={{ pathname: urls.find((url) => isURLAvailable(url)) ?? elseRedirect ?? '/access_denied' }} />
<Redirect to={{ pathname: urls.find((url) => isURLAvailable(url)) ?? elseRedirect ?? (getUserId() ? '/access_denied' : '/login') }} />
</Route>
))

View File

@ -3,6 +3,7 @@ import { memo, ReactNode } from 'react'
import { Redirect, Route, RouteProps } from 'react-router-dom'
import { join } from 'path'
import { getUserId } from '@utils/storage'
import { isURLAvailable } from '@utils/permissions'
export type PrivateRouteProps = RouteProps & {
@ -13,7 +14,7 @@ export type PrivateRouteProps = RouteProps & {
}
export const defaultRedirect = (location?: Location<unknown>) => (
<Redirect to={{ pathname: '/access_denied', state: { from: location } }} />
<Redirect to={{ pathname: getUserId() ? '/access_denied' : '/login', state: { from: location } }} />
)
export const PrivateRoute = memo<PrivateRouteProps>(({ root = '', path, component, children, redirect = defaultRedirect, ...other }) => {