import { createRouter, createWebHistory } from 'vue-router' import BooksBrowser from './BooksBrowser.vue' import AddBook from './AddBook.vue' import AuthorForm from './AuthorForm.vue' import BookForm from './BookForm.vue' import SignUp from './SignUp.vue' import LogIn from './LogIn.vue' import Home from './Home.vue' import ScanBook from './ScanBook.vue' import SearchBook from './SearchBook.vue' import ImportInventaire from './ImportInventaire.vue' import InstanceBrowser from './InstanceBrowser.vue' import { useAuthStore } from './auth.store' const routes = [ { path: '/', component: Home }, { path: '/scan', component: ScanBook }, { path: '/browse', component: InstanceBrowser }, { path: '/books', component: BooksBrowser }, { path: '/book/:id', component: BookForm, props: true }, { path: '/author/:id', component: AuthorForm, props: true }, { path: '/search/:searchterm', component: SearchBook, props: true }, { path: '/import/inventaire/:inventaireid', component: ImportInventaire, props: true }, { path: '/add', component: AddBook }, { path: '/signup', component: SignUp }, { path: '/login', component: LogIn }, ] export const router = createRouter({ history: createWebHistory(), routes, }) router.beforeEach(async (to) => { // redirect to login page if not logged in and trying to access a restricted page const publicPages = ['/', '/login', '/signup'] const authRequired = !publicPages.includes(to.path) const auth = useAuthStore() if (authRequired && !auth.user) { auth.returnUrl = to.fullPath return '/login' } })