Files
webMontime/src/router/index.ts
T
2026-05-18 20:18:28 +03:30

121 lines
3.3 KiB
TypeScript

import { createRouter, createWebHistory } from 'vue-router';
import { getAppMode } from '@/composables/useAppMode';
import { useAuthStore } from '@/stores/auth';
const mode = getAppMode();
const webRoutes = [
{ path: '/', redirect: '/login' },
{
path: '/login',
name: 'login',
component: () => import('@/views/auth/LoginView.vue'),
meta: { guest: true, shell: 'auth' },
},
{
path: '/verify',
name: 'verify',
component: () => import('@/views/auth/VerifyOtpView.vue'),
meta: { guest: true, shell: 'auth' },
},
{
path: '/app',
name: 'app-home',
component: () => import('@/views/app/AppHomeView.vue'),
meta: { requiresAuth: true, shell: 'web' },
},
{
path: '/app/bookings',
name: 'app-bookings',
component: () => import('@/views/app/AppBookingsView.vue'),
meta: { requiresAuth: true, shell: 'web' },
},
{
path: '/app/profile',
name: 'app-profile',
component: () => import('@/views/app/AppProfileView.vue'),
meta: { requiresAuth: true, shell: 'web' },
},
{
path: '/app/owner',
name: 'app-owner',
component: () => import('@/views/app/AppOwnerView.vue'),
meta: { requiresAuth: true, requiresBusinessOwner: true, shell: 'web' },
},
{ path: '/home', redirect: '/app' },
];
const adminRoutes = [
{ path: '/', redirect: '/admin/login' },
{
path: '/admin/login',
name: 'admin-login',
component: () => import('@/views/auth/LoginView.vue'),
meta: { guest: true, shell: 'auth', adminAuth: true },
},
{
path: '/admin/verify',
name: 'admin-verify',
component: () => import('@/views/auth/VerifyOtpView.vue'),
meta: { guest: true, shell: 'auth', adminAuth: true },
},
{
path: '/admin',
name: 'admin-home',
component: () => import('@/views/admin/AdminHomeView.vue'),
meta: { requiresAuth: true, requiresAdmin: true, shell: 'admin' },
},
{
path: '/admin/businesses',
name: 'admin-businesses',
component: () => import('@/views/admin/AdminBusinessesView.vue'),
meta: { requiresAuth: true, requiresAdmin: true, shell: 'admin' },
},
{
path: '/admin/users',
name: 'admin-users',
component: () => import('@/views/admin/AdminUsersView.vue'),
meta: { requiresAuth: true, requiresAdmin: true, shell: 'admin' },
},
];
const router = createRouter({
history: createWebHistory(),
routes: mode === 'admin' ? adminRoutes : webRoutes,
});
router.beforeEach(async (to) => {
const auth = useAuthStore();
const appMode = getAppMode();
if (auth.token && !auth.user) {
await auth.loadUser();
}
if (to.meta.requiresAuth && !auth.isLoggedIn) {
return appMode === 'admin' ? { name: 'admin-login' } : { name: 'login' };
}
if (to.meta.requiresAdmin && !auth.isAdmin) {
return appMode === 'admin' ? { name: 'admin-login' } : { name: 'login' };
}
if (to.meta.requiresBusinessOwner && !auth.isBusinessOwner && !auth.isAdmin) {
return { name: 'app-home' };
}
if (to.meta.guest && auth.isLoggedIn) {
if (to.meta.adminAuth) {
return auth.isAdmin ? { name: 'admin-home' } : undefined;
}
return { name: 'app-home' };
}
if (appMode === 'admin' && auth.isLoggedIn && !auth.isAdmin && !to.meta.guest) {
auth.logout();
return { name: 'admin-login' };
}
});
export default router;