You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
const isAdminDeploy = import.meta.env.MODE === 'admin'
const router = createRouter({
history: isAdminDeploy
? createWebHashHistory(import.meta.env.BASE_URL)
: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/login',
name: 'login',
component: () => import('@/views/login/index.vue'),
meta: { public: true, title: '登录' },
},
],
})
router.beforeEach(async (to, _from, next) => {
const auth = useAuthStore()
if (to.meta.public) {
if (auth.token && to.name === 'login') {
return next({ path: '/dashboard' })
}
return next()
}
if (!auth.token) {
return next({ name: 'login', query: { redirect: to.fullPath } })
}
if (!auth.routesAdded) {
try {
await auth.bootstrap(router)
return next({ ...to, replace: true })
} catch {
auth.clearAuth()
return next({ name: 'login' })
}
}
return next()
})
export default router