2026-08-11 23:17:04 +07:00
|
|
|
import { createRouter, createWebHistory } from 'vue-router'
|
|
|
|
|
import { useAuthStore } from '../stores/auth'
|
|
|
|
|
|
|
|
|
|
declare module 'vue-router' {
|
|
|
|
|
interface RouteMeta {
|
|
|
|
|
public?: boolean
|
|
|
|
|
screenKey?: string
|
2026-08-24 08:07:23 +07:00
|
|
|
title?: string
|
2026-09-12 19:38:11 +07:00
|
|
|
adminOnly?: boolean
|
2026-08-11 23:17:04 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-12 19:38:11 +07:00
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 23:17:04 +07:00
|
|
|
const router = createRouter({
|
|
|
|
|
history: createWebHistory(),
|
|
|
|
|
routes: [
|
|
|
|
|
{
|
|
|
|
|
path: '/login',
|
|
|
|
|
name: 'login',
|
|
|
|
|
component: () => import('../views/auth/LoginView.vue'),
|
|
|
|
|
meta: { public: true },
|
|
|
|
|
},
|
2026-08-26 00:11:45 +07:00
|
|
|
{
|
|
|
|
|
path: '/select-project',
|
|
|
|
|
name: 'select-project',
|
|
|
|
|
component: () => import('../views/projects/ProjectSelectView.vue'),
|
|
|
|
|
},
|
2026-08-11 23:17:04 +07:00
|
|
|
{
|
|
|
|
|
path: '/',
|
2026-09-12 19:38:11 +07:00
|
|
|
component: () => import('../layouts/AdminLayout.vue'),
|
2026-08-11 23:17:04 +07:00
|
|
|
children: [
|
2026-09-12 19:38:11 +07:00
|
|
|
{ path: '', redirect: () => landingPath() },
|
|
|
|
|
{ path: 'projects', name: 'projects', component: () => import('../views/projects/ProjectsListView.vue'), meta: { screenKey: 'projects', title: 'Projects', adminOnly: true } },
|
2026-08-24 08:07:23 +07:00
|
|
|
{ path: 'users', name: 'users', component: () => import('../views/users/UsersView.vue'), meta: { screenKey: 'users', title: 'Users' } },
|
2026-08-26 00:11:45 +07:00
|
|
|
{
|
|
|
|
|
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' } },
|
|
|
|
|
],
|
|
|
|
|
},
|
2026-08-11 23:17:04 +07:00
|
|
|
],
|
|
|
|
|
},
|
2026-09-12 19:38:11 +07:00
|
|
|
{
|
|
|
|
|
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() },
|
2026-08-11 23:17:04 +07:00
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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) {
|
2026-09-12 19:38:11 +07:00
|
|
|
return landingPath()
|
2026-08-11 23:17:04 +07:00
|
|
|
}
|
|
|
|
|
if (auth.isAuthenticated) {
|
2026-09-12 19:38:11 +07:00
|
|
|
try {
|
|
|
|
|
await auth.ensureMenu()
|
|
|
|
|
} catch {
|
|
|
|
|
// never block navigation on a menu fetch failure
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (to.meta.adminOnly && !auth.isAdmin) {
|
|
|
|
|
return fallbackLocation()
|
2026-08-11 23:17:04 +07:00
|
|
|
}
|
|
|
|
|
if (to.meta.screenKey && !auth.canView(to.meta.screenKey)) {
|
2026-09-12 19:38:11 +07:00
|
|
|
return fallbackLocation()
|
2026-08-11 23:17:04 +07:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default router
|