Files
mws.frontend.vue/src/router/index.ts
T
2026-09-12 19:38:11 +07:00

97 lines
3.5 KiB
TypeScript

import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../stores/auth'
declare module 'vue-router' {
interface RouteMeta {
public?: boolean
screenKey?: string
title?: string
adminOnly?: boolean
}
}
function landingPath(): string {
const auth = useAuthStore()
return auth.isAdmin ? '/projects' : '/select-project'
}
function fallbackLocation() {
const last = localStorage.getItem('mws_last_project')
if (last) return { name: 'tasks-board', params: { id: last } }
return landingPath()
}
const router = createRouter({
history: createWebHistory(),
routes: [
{
path: '/login',
name: 'login',
component: () => import('../views/auth/LoginView.vue'),
meta: { public: true },
},
{
path: '/select-project',
name: 'select-project',
component: () => import('../views/projects/ProjectSelectView.vue'),
},
{
path: '/',
component: () => import('../layouts/AdminLayout.vue'),
children: [
{ path: '', redirect: () => landingPath() },
{ path: 'projects', name: 'projects', component: () => import('../views/projects/ProjectsListView.vue'), meta: { screenKey: 'projects', title: 'Projects', adminOnly: true } },
{ path: 'users', name: 'users', component: () => import('../views/users/UsersView.vue'), meta: { screenKey: 'users', title: 'Users' } },
{
path: 'settings',
component: () => import('../views/settings/SettingsView.vue'),
meta: { title: 'Settings' },
children: [
{ path: '', redirect: '/settings/masterdata' },
{ path: 'masterdata', name: 'settings-masterdata', component: () => import('../views/settings/masterdata/MasterDataView.vue'), meta: { title: 'Master Data', screenKey: 'masterdata' } },
{ path: 'roles', name: 'settings-roles', component: () => import('../views/settings/roles/RolesView.vue'), meta: { title: 'Roles', screenKey: 'permissions' } },
{ path: 'permissions', name: 'settings-permissions', component: () => import('../views/settings/permissions/PermissionsView.vue'), meta: { title: 'Permissions', screenKey: 'permissions' } },
],
},
],
},
{
path: '/projects/:id',
component: () => import('../layouts/MainLayout.vue'),
meta: { screenKey: 'projects' },
children: [
{ path: '', redirect: (to) => ({ name: 'tasks-board', params: { id: to.params.id } }) },
{ path: 'documents', name: 'documents', component: () => import('../views/documents/DocumentsView.vue'), meta: { screenKey: 'documents' } },
{ path: 'tasks/board', name: 'tasks-board', component: () => import('../views/tasks/TasksBoardView.vue'), meta: { screenKey: 'tasks' } },
{ path: 'members', name: 'members', component: () => import('../views/projects/MembersView.vue') },
],
},
{ path: '/:pathMatch(.*)*', redirect: () => landingPath() },
],
})
router.beforeEach(async (to) => {
const auth = useAuthStore()
if (!to.meta.public && !auth.isAuthenticated) {
return { name: 'login', query: { redirect: to.fullPath } }
}
if (to.name === 'login' && auth.isAuthenticated) {
return landingPath()
}
if (auth.isAuthenticated) {
try {
await auth.ensureMenu()
} catch {
// never block navigation on a menu fetch failure
}
}
if (to.meta.adminOnly && !auth.isAdmin) {
return fallbackLocation()
}
if (to.meta.screenKey && !auth.canView(to.meta.screenKey)) {
return fallbackLocation()
}
})
export default router