45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
import AppLayout from '@/layouts/AppLayout.vue'
|
|
import { useAuthStore } from '@/modules/auth/public'
|
|
import { getFirstAccessiblePath } from '@/navigation/clientNavigation'
|
|
import { authRoutes } from '@/modules/auth/routes'
|
|
import { activeWorkspaceRoutes } from '@/modules/registry'
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(),
|
|
routes: [
|
|
...authRoutes,
|
|
{
|
|
path: '/',
|
|
component: AppLayout,
|
|
meta: { requiresAuth: true },
|
|
children: activeWorkspaceRoutes,
|
|
},
|
|
{ path: '/:pathMatch(.*)*', redirect: '/' },
|
|
],
|
|
})
|
|
|
|
router.beforeEach(async (to) => {
|
|
document.title = to.meta.title ? `${to.meta.title} - 柏悦物流客户工作台` : '柏悦物流客户工作台'
|
|
const auth = useAuthStore()
|
|
if (!auth.sessionRestored) {
|
|
try {
|
|
await auth.restoreSession()
|
|
} catch {
|
|
return { name: 'login', query: { redirect: to.fullPath } }
|
|
}
|
|
}
|
|
if (to.meta.requiresAuth && !auth.isAuthenticated) {
|
|
return { name: 'login', query: { redirect: to.fullPath } }
|
|
}
|
|
if ((to.name === 'login' || to.name === 'register') && auth.isAuthenticated) {
|
|
return getFirstAccessiblePath(auth.permissionCodes)
|
|
}
|
|
const permission = typeof to.meta.permission === 'string' ? to.meta.permission : undefined
|
|
if (auth.isAuthenticated && permission && !auth.hasPermission(permission)) {
|
|
return { name: 'forbidden', query: { from: to.fullPath } }
|
|
}
|
|
})
|
|
|
|
export default router
|