Files
oauth/resources/js/router/index.ts
2026-07-27 17:20:02 +02:00

121 lines
3.1 KiB
TypeScript

import { createRouter, createWebHistory } from 'vue-router';
import { useAuthStore } from '../stores/auth';
declare module 'vue-router' {
interface RouteMeta {
requiresAuth?: boolean;
requiresAdmin?: boolean;
guestOnly?: boolean;
}
}
const routes = [
{
path: '/',
name: 'dashboard',
component: () => import('../pages/Dashboard.vue'),
meta: { requiresAuth: true },
},
{
path: '/login',
name: 'login',
component: () => import('../pages/Login.vue'),
meta: { guestOnly: true },
},
{
path: '/register',
name: 'register',
component: () => import('../pages/Register.vue'),
meta: { guestOnly: true },
},
{
path: '/forgot-password',
name: 'forgot-password',
component: () => import('../pages/ForgotPassword.vue'),
meta: { guestOnly: true },
},
{
path: '/reset-password',
name: 'reset-password',
component: () => import('../pages/ResetPassword.vue'),
meta: { guestOnly: true },
},
{
path: '/clients',
name: 'clients',
component: () => import('../pages/Clients.vue'),
meta: { requiresAuth: true },
},
{
path: '/tokens',
name: 'tokens',
component: () => import('../pages/Tokens.vue'),
meta: { requiresAuth: true },
},
{
path: '/authorized-apps',
name: 'authorized-apps',
component: () => import('../pages/AuthorizedApps.vue'),
meta: { requiresAuth: true },
},
{
path: '/security',
name: 'security',
component: () => import('../pages/Security.vue'),
meta: { requiresAuth: true },
},
{
path: '/oauth/authorize',
name: 'oauth-authorize',
component: () => import('../pages/Authorize.vue'),
meta: { requiresAuth: true },
},
{
path: '/admin/users',
name: 'admin-users',
component: () => import('../pages/admin/Users.vue'),
meta: { requiresAuth: true, requiresAdmin: true },
},
{
path: '/admin/analytics',
name: 'admin-analytics',
component: () => import('../pages/admin/Analytics.vue'),
meta: { requiresAuth: true, requiresAdmin: true },
},
{
path: '/admin/audit-log',
name: 'admin-audit-log',
component: () => import('../pages/admin/AuditLog.vue'),
meta: { requiresAuth: true, requiresAdmin: true },
},
];
const router = createRouter({
history: createWebHistory(),
routes,
});
router.beforeEach(async (to) => {
const auth = useAuthStore();
if (!auth.checked) {
await auth.fetchUser();
}
if (to.meta.requiresAuth && !auth.isAuthenticated) {
return { name: 'login', query: { redirect: to.fullPath } };
}
if (to.meta.requiresAdmin && !auth.isAdmin) {
return { name: 'dashboard' };
}
if (to.meta.guestOnly && auth.isAuthenticated) {
return { name: 'dashboard' };
}
return true;
});
export default router;