82 lines
2.3 KiB
JavaScript
82 lines
2.3 KiB
JavaScript
import { createRouter, createWebHistory } from 'vue-router';
|
|
import { getCookie } from '../utils/functions.js';
|
|
|
|
const routes = [
|
|
{
|
|
path: '/login',
|
|
name: 'Login',
|
|
component: () => import('../views/Login.vue'),
|
|
meta: { requiresAuth: false }
|
|
},
|
|
{
|
|
path: '/',
|
|
name: 'Home',
|
|
component: () => import('../views/Home.vue'),
|
|
meta: { requiresAuth: true },
|
|
redirect: '/instances',
|
|
children: [
|
|
{
|
|
path: 'instances',
|
|
name: 'Instances',
|
|
component: () => import('../views/Instances.vue')
|
|
},
|
|
{
|
|
path: 'instances/:id',
|
|
name: 'InstanceDetail',
|
|
component: () => import('../views/InstanceDetail.vue')
|
|
},
|
|
{
|
|
path: 'users',
|
|
name: 'Users',
|
|
component: () => import('../views/Users.vue'),
|
|
meta: { requiresAuth: true, permission: ['superuser'] }
|
|
},
|
|
{
|
|
path: 'sessions',
|
|
name: 'Sessions',
|
|
component: () => import('../views/Sessions.vue'),
|
|
meta: { requiresAuth: true, permission: ['superuser', 'admin'] }
|
|
},
|
|
{
|
|
path: 'logs',
|
|
name: 'Logs',
|
|
component: () => import('../views/Logs.vue'),
|
|
meta: { requiresAuth: true, permission: ['superuser', 'admin'] }
|
|
},
|
|
{
|
|
path: 'settings',
|
|
name: 'Settings',
|
|
component: () => import('../views/Settings.vue'),
|
|
meta: { requiresAuth: true, permission: ['superuser', 'admin'] }
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(),
|
|
routes
|
|
});
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
const token = getCookie('token');
|
|
const userType = getCookie('user-type');
|
|
|
|
if (to.meta.requiresAuth && !token) {
|
|
next('/login');
|
|
} else if (to.path === '/login' && token) {
|
|
next('/');
|
|
} else if (to.meta.permission) {
|
|
const permissions = to.meta.permission;
|
|
if (!permissions.includes(userType)) {
|
|
next('/instances');
|
|
} else {
|
|
next();
|
|
}
|
|
} else {
|
|
next();
|
|
}
|
|
});
|
|
|
|
export default router;
|