feat: 新增路由鉴权

This commit is contained in:
duanshuwen
2025-12-16 21:24:42 +08:00
parent cadf0d8098
commit c9a96f1b06
6 changed files with 84 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
import router from './router'
import { isPathMatch } from '@utils/validate'
import { getToken } from '@utils/auth'
// 白名单
const whiteList = ['/login', '/register']
const isWhiteList = (path: string) => whiteList.some(pattern => isPathMatch(pattern, path))
router.beforeEach((to: any, _from: any, next: any) => {
if(getToken()) {
// has token
if (to.path === '/login') {
next({path: '/home'})
} else if (isWhiteList(to.path)) {
next()
} else {
}
} else {
// no token
if (isWhiteList(to.path)) {
// 在免登录白名单,直接进入
next()
} else {
next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
}
}
})