Files
bibliomane/front/src/router.js

51 lines
1.9 KiB
JavaScript

import { createRouter, createWebHistory } from 'vue-router'
import BooksBrowser from './BooksBrowser.vue'
import CollectionsBrowser from './CollectionsBrowser.vue'
import CollectionForm from './CollectionForm.vue'
import BookFormEdit from './BookFormEdit.vue'
import AuthorForm from './AuthorForm.vue'
import BookFormView from './BookFormView.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: BookFormView, props: true },
{ path: '/book/:id/edit', component: BookFormEdit, props: true },
{ path: '/collections', component: CollectionsBrowser },
{ path: '/collection/:id', component: CollectionForm, 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: BookFormEdit },
{ 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'
}
})