import { createRouter, createWebHistory } from 'vue-router' import { useAuthStore } from '@/stores/authStore' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), scrollBehavior: () => ({ top: 0 }), routes: [ { path: '/', component: () => import('@/layouts/PublicLayout.vue'), children: [ { path: '', name: 'home', component: () => import('@/views/public/HomeView.vue'), }, { path: 'login', name: 'login', component: () => import('@/views/public/LoginView.vue'), }, { path: 'primeiro-acesso', name: 'primeiro-acesso', component: () => import('@/views/public/PrimeiroAcessoView.vue'), }, { path: 'credenciamento', name: 'credenciamento', component: () => import('@/views/public/CredenciamentoView.vue'), }, { path: 'servicos', name: 'servicos', component: () => import('@/views/servicos/ServicosHubView.vue'), }, { path: 'servicos/certidao', name: 'certidao', component: () => import('@/views/servicos/CertidaoView.vue'), }, { path: 'servicos/iptu', name: 'iptu', component: () => import('@/views/servicos/IptuView.vue'), }, ], }, { path: '/portal', component: () => import('@/layouts/PortalLayout.vue'), meta: { requiresAuth: true }, children: [ { path: '', redirect: { name: 'painel' }, }, { path: 'painel', name: 'painel', component: () => import('@/views/portal/PainelView.vue'), }, { path: 'debitos', name: 'debitos', component: () => import('@/views/portal/DebitosView.vue'), }, { path: 'certidoes', name: 'certidoes-portal', component: () => import('@/views/portal/CertidoesView.vue'), }, { path: 'alvaras', name: 'alvaras', component: () => import('@/views/portal/AlvarasView.vue'), }, { path: 'pagamentos', name: 'pagamentos', component: () => import('@/views/portal/PagamentosView.vue'), }, { path: 'dados', name: 'dados', component: () => import('@/views/portal/DadosView.vue'), }, ], }, { path: '/:pathMatch(.*)*', redirect: { name: 'home' }, }, ], }) router.beforeEach((to) => { if (!to.meta.requiresAuth) return true const auth = useAuthStore() if (!auth.isAuthenticated) { return { name: 'home' } } }) export default router