优化前端认证与类型管理
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
|
||||
import { useAdminSessionStore } from '@/stores/adminSession'
|
||||
import { useSessionStore } from '@/stores/session'
|
||||
import { getAccessToken, getLoginPath } from '@/utils/authStorage'
|
||||
import { accountRoutes } from './accountRoutes'
|
||||
import { adminRoutes } from './adminRoutes'
|
||||
import { isMobileHost, toMobilePath } from './mobileHost'
|
||||
@@ -26,16 +28,17 @@ router.beforeEach(async (to) => {
|
||||
|
||||
if (to.meta.requiresAuth) {
|
||||
const session = useSessionStore()
|
||||
const hasToken = !!localStorage.getItem('access_token')
|
||||
session.syncFromStorage()
|
||||
const hasToken = !!session.token
|
||||
if (!hasToken) {
|
||||
const loginPath = to.path.startsWith('/m') ? '/m/login' : '/login'
|
||||
const loginPath = getLoginPath('user', to.path)
|
||||
return { path: loginPath, query: { redirect: to.fullPath } }
|
||||
}
|
||||
if (!session.phone) {
|
||||
try {
|
||||
await session.loadMe()
|
||||
} catch {
|
||||
const loginPath = to.path.startsWith('/m') ? '/m/login' : '/login'
|
||||
const loginPath = getLoginPath('user', to.path)
|
||||
return { path: loginPath, query: { redirect: to.fullPath } }
|
||||
}
|
||||
}
|
||||
@@ -43,14 +46,15 @@ router.beforeEach(async (to) => {
|
||||
|
||||
if (to.meta.requiresRealname) {
|
||||
const session = useSessionStore()
|
||||
const loginPath = to.path.startsWith('/m') ? '/m/login' : '/login'
|
||||
session.syncFromStorage()
|
||||
const loginPath = getLoginPath('user', to.path)
|
||||
const realnamePath = to.path.startsWith('/m') ? '/m/realname' : '/realname'
|
||||
|
||||
if (session.realnameStatus !== 'verified') {
|
||||
try {
|
||||
await session.loadMe()
|
||||
} catch {
|
||||
if (!localStorage.getItem('access_token')) {
|
||||
if (!getAccessToken('user')) {
|
||||
return { path: loginPath, query: { redirect: to.fullPath } }
|
||||
}
|
||||
}
|
||||
@@ -61,12 +65,13 @@ router.beforeEach(async (to) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (to.path === '/admin/login' && localStorage.getItem('admin_access_token')) {
|
||||
const adminSession = useAdminSessionStore()
|
||||
adminSession.syncFromStorage()
|
||||
if (to.path === '/admin/login' && adminSession.token) {
|
||||
return '/admin/dashboard'
|
||||
}
|
||||
if (to.meta.requiresAdmin) {
|
||||
const token = localStorage.getItem('admin_access_token')
|
||||
if (!token) {
|
||||
if (!adminSession.token) {
|
||||
return '/admin/login'
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user