85 lines
2.2 KiB
TypeScript
85 lines
2.2 KiB
TypeScript
import { createRouter, createWebHistory } from 'vue-router';
|
|
import { useAuthStore } from '@/stores/auth';
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(),
|
|
routes: [
|
|
{ path: '/', redirect: '/login' },
|
|
{
|
|
path: '/login',
|
|
name: 'login',
|
|
component: () => import('@/views/auth/LoginView.vue'),
|
|
meta: { guest: true },
|
|
},
|
|
{
|
|
path: '/verify',
|
|
name: 'verify',
|
|
component: () => import('@/views/auth/VerifyOtpView.vue'),
|
|
meta: { guest: true },
|
|
},
|
|
{
|
|
path: '/app',
|
|
name: 'app-home',
|
|
component: () => import('@/views/app/AppHomeView.vue'),
|
|
meta: { requiresAuth: true },
|
|
},
|
|
{
|
|
path: '/app/businesses/:id',
|
|
name: 'app-business-detail',
|
|
component: () => import('@/views/app/AppBusinessDetailView.vue'),
|
|
meta: { requiresAuth: true },
|
|
},
|
|
{
|
|
path: '/app/bookings',
|
|
name: 'app-bookings',
|
|
component: () => import('@/views/app/AppBookingsView.vue'),
|
|
meta: { requiresAuth: true },
|
|
},
|
|
{
|
|
path: '/app/profile',
|
|
name: 'app-profile',
|
|
component: () => import('@/views/app/AppProfileView.vue'),
|
|
meta: { requiresAuth: true },
|
|
},
|
|
{
|
|
path: '/app/register-business',
|
|
name: 'app-register-business',
|
|
component: () => import('@/views/app/AppRegisterBusinessView.vue'),
|
|
meta: { requiresAuth: true, requiresCustomer: true },
|
|
},
|
|
{
|
|
path: '/app/owner',
|
|
name: 'app-owner',
|
|
component: () => import('@/views/app/AppOwnerView.vue'),
|
|
meta: { requiresAuth: true, requiresBusinessOwner: true },
|
|
},
|
|
{ path: '/home', redirect: '/app' },
|
|
],
|
|
});
|
|
|
|
router.beforeEach(async (to) => {
|
|
const auth = useAuthStore();
|
|
|
|
if (auth.token) {
|
|
await auth.loadUser();
|
|
}
|
|
|
|
if (to.meta.requiresAuth && !auth.isLoggedIn) {
|
|
return { name: 'login' };
|
|
}
|
|
|
|
if (to.meta.requiresBusinessOwner && !auth.isBusinessOwner && !auth.isAdmin) {
|
|
return { name: 'app-home' };
|
|
}
|
|
|
|
if (to.meta.requiresCustomer && !auth.isCustomer) {
|
|
return auth.isBusinessOwner ? { name: 'app-owner' } : { name: 'app-home' };
|
|
}
|
|
|
|
if (to.meta.guest && auth.isLoggedIn) {
|
|
return { name: 'app-home' };
|
|
}
|
|
});
|
|
|
|
export default router;
|