import { createRouter, createWebHistory } from 'vue-router' import { useAuthStore } from '@/stores/auth' const routes = [ { path: '/login', name: 'Login', component: () => import('../views/LoginView.vue'), meta: { title: 'Conectare', requiresAuth: false } }, { path: '/', name: 'ReceiptsList', component: () => import('../views/receipts/ReceiptsListView.vue'), meta: { title: 'Lista Bonuri', requiresAuth: true } }, { path: '/create', name: 'ReceiptCreate', component: () => import('../views/receipts/ReceiptCreateView.vue'), meta: { title: 'Bon Nou', requiresAuth: true } }, { path: '/receipt/:id', name: 'ReceiptDetail', component: () => import('../views/receipts/ReceiptDetailView.vue'), meta: { title: 'Detalii Bon', requiresAuth: true } }, { path: '/receipt/:id/edit', name: 'ReceiptEdit', component: () => import('../views/receipts/ReceiptCreateView.vue'), meta: { title: 'Editare Bon', requiresAuth: true } }, { path: '/approval', name: 'ReceiptApproval', component: () => import('../views/receipts/ReceiptApprovalView.vue'), meta: { title: 'Aprobare Bonuri', requiresAuth: true } } ] const router = createRouter({ history: createWebHistory(), routes }) // Authentication guard and page title router.beforeEach((to, from, next) => { // Update page title document.title = to.meta.title ? `${to.meta.title} | Data Entry` : 'Data Entry - Bonuri Fiscale' // Check authentication const authStore = useAuthStore() const requiresAuth = to.meta.requiresAuth !== false if (requiresAuth && !authStore.isAuthenticated) { // Redirect to login if not authenticated next({ name: 'Login', query: { redirect: to.fullPath } }) } else if (to.name === 'Login' && authStore.isAuthenticated) { // Redirect to home if already authenticated next({ name: 'ReceiptsList' }) } else { next() } }) export default router