Skip to content

Commit

Permalink
feat: 完成登录路由拦截功能
Browse files Browse the repository at this point in the history
  • Loading branch information
baimingxuan committed Nov 28, 2023
1 parent 0526aa6 commit d3ecc00
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 29 deletions.
10 changes: 3 additions & 7 deletions src/layout/feature/components/UserDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { MenuProps } from 'antd'
import { Space, Dropdown } from 'antd'
import { LockOutlined, PoweroffOutlined } from '@ant-design/icons'
import { useNavigate } from 'react-router-dom'
import { getAuthCache } from '@/utils/auth'
import { getAuthCache, clearAuthCache } from '@/utils/auth'
import { TOKEN_KEY } from '@/enums/cacheEnum'
import { useAppDispatch, useAppSelector } from '@/stores'
import { useMessage } from '@/hooks/web/useMessage'
Expand All @@ -11,7 +11,6 @@ import { resetState } from '@/stores/modules/userSlice'
import headerImg from '@/assets/images/avatar.png'

export default function UserDropdown() {

const items: MenuProps['items'] = [
{
key: 'lock',
Expand Down Expand Up @@ -52,7 +51,6 @@ export default function UserDropdown() {
return token || getAuthCache<string>(TOKEN_KEY)
}


const handleLock = () => {}

const handleLogout = () => {
Expand All @@ -78,15 +76,13 @@ export default function UserDropdown() {
}
}
dispatch(resetState())
clearAuthCache()
goLogin && navigate('/login')
}

return (
<Dropdown menu={{ items, onClick }} placement='bottomRight' arrow>
<span
className='flex-center'
style={{cursor: 'pointer'}}
>
<span className='flex-center' style={{ cursor: 'pointer' }}>
<img
src={headerImg}
style={{
Expand Down
10 changes: 10 additions & 0 deletions src/router/constant.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BasicLayout } from '@/layout'
import { GuardRoute } from './guardRoute'

export const LayoutGuard = () => {
return (
<GuardRoute>
<BasicLayout />
</GuardRoute>
)
}
17 changes: 17 additions & 0 deletions src/router/guardRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Navigate } from 'react-router-dom'
import { getAuthCache } from '@/utils/auth'
import { TOKEN_KEY } from '@/enums/cacheEnum'
import { useAppSelector } from '@/stores'

export const GuardRoute = (props: { children: JSX.Element }) => {
const { token } = useAppSelector(state => state.user)
const getToken = (): string => {
return token || getAuthCache<string>(TOKEN_KEY)
}

if (getToken()) {
return props.children
} else {
return <Navigate to='/login' replace />
}
}
4 changes: 2 additions & 2 deletions src/router/routes/compo.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { lazy } from 'react'
import { RouteObject } from '../types'
import { BasicLayout } from '@/layout'
import { LayoutGuard } from '../constant'
import lazyLoad from '../lazyLoad'

// component module page
const CompoRoute: RouteObject = {
path: '/compo',
element: <BasicLayout />,
element: <LayoutGuard />,
meta: {
title: '组件',
icon: 'compo',
Expand Down
4 changes: 2 additions & 2 deletions src/router/routes/excel.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { lazy } from 'react'
import { RouteObject } from '../types'
import { BasicLayout } from '@/layout'
import { LayoutGuard } from '../constant'
import lazyLoad from '../lazyLoad'

// excel module page
const ExcelRoute: RouteObject = {
path: '/excel',
element: <BasicLayout />,
element: <LayoutGuard />,
meta: {
title: 'Excel',
icon: 'excel',
Expand Down
4 changes: 2 additions & 2 deletions src/router/routes/exception.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { lazy } from 'react'
import { RouteObject } from '../types'
import { BasicLayout } from '@/layout'
import { LayoutGuard } from '../constant'
import lazyLoad from '../lazyLoad'

// exception module page
const ExceptionRoute: RouteObject = {
path: '/exception',
element: <BasicLayout />,
element: <LayoutGuard />,
meta: {
title: '异常页面',
icon: 'bug',
Expand Down
4 changes: 2 additions & 2 deletions src/router/routes/form.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { lazy } from 'react'
import { RouteObject } from '../types'
import { BasicLayout } from '@/layout'
import { LayoutGuard } from '../constant'
import lazyLoad from '../lazyLoad'

// form module page
const FormRoute: RouteObject = {
path: '/form',
element: <BasicLayout />,
element: <LayoutGuard />,
meta: {
title: '表单',
icon: 'form',
Expand Down
4 changes: 2 additions & 2 deletions src/router/routes/graph-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { lazy } from 'react'
import { RouteObject } from '../types'
import { BasicLayout } from '@/layout'
import { LayoutGuard } from '../constant'
import lazyLoad from '../lazyLoad'

// graph-editor module page
const GraphEditorRoute: RouteObject = {
path: '/graph-editor',
element: <BasicLayout />,
element: <LayoutGuard />,
meta: {
title: '图形编辑器',
icon: 'flow',
Expand Down
4 changes: 2 additions & 2 deletions src/router/routes/home.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { RouteObject } from '../types'
import { BasicLayout } from '@/layout'
import { LayoutGuard } from '../constant'
import Home from '@/views/home'

// Home route
const HomeRoute: RouteObject = {
path: '/home',
element: <BasicLayout />,
element: <LayoutGuard />,
meta: {
title: '首页',
icon: 'home',
Expand Down
4 changes: 2 additions & 2 deletions src/router/routes/images.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { lazy } from 'react'
import { RouteObject } from '../types'
import { BasicLayout } from '@/layout'
import { LayoutGuard } from '../constant'
import lazyLoad from '../lazyLoad'

// image module page
const ImageRoute: RouteObject = {
path: '/image',
element: <BasicLayout />,
element: <LayoutGuard />,
meta: {
title: '图片处理',
icon: 'image',
Expand Down
4 changes: 2 additions & 2 deletions src/router/routes/table.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { lazy } from 'react'
import { RouteObject } from '../types'
import { BasicLayout } from '@/layout'
import { LayoutGuard } from '../constant'
import lazyLoad from '../lazyLoad'

// table module page
const TableRoute: RouteObject = {
path: '/table',
element: <BasicLayout />,
element: <LayoutGuard />,
meta: {
title: '表格',
icon: 'table',
Expand Down
4 changes: 2 additions & 2 deletions src/router/routes/text-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { lazy } from 'react'
import { RouteObject } from '../types'
import { BasicLayout } from '@/layout'
import { LayoutGuard } from '../constant'
import lazyLoad from '../lazyLoad'

// text-editor module page
const TextEditorRoute: RouteObject = {
path: '/editor',
element: <BasicLayout />,
element: <LayoutGuard />,
meta: {
title: '文本编辑器',
icon: 'editor',
Expand Down
4 changes: 2 additions & 2 deletions src/router/routes/tree.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { lazy } from 'react'
import { RouteObject } from '../types'
import { BasicLayout } from '@/layout'
import { LayoutGuard } from '../constant'
import lazyLoad from '../lazyLoad'

// tree module page
const TreeRoute: RouteObject = {
path: '/tree',
element: <BasicLayout />,
element: <LayoutGuard />,
meta: {
title: '树形结构',
icon: 'tree',
Expand Down
4 changes: 2 additions & 2 deletions src/router/routes/video.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { lazy } from 'react'
import { RouteObject } from '../types'
import { BasicLayout } from '@/layout'
import { LayoutGuard } from '../constant'
import lazyLoad from '../lazyLoad'

// video module page
const VideoRoute: RouteObject = {
path: '/video',
element: <BasicLayout />,
element: <LayoutGuard />,
meta: {
title: '视频处理',
icon: 'video',
Expand Down

0 comments on commit d3ecc00

Please sign in to comment.