diff --git a/front/src/router.js b/front/src/router.js index e3a25ed..3213bba 100644 --- a/front/src/router.js +++ b/front/src/router.js @@ -5,6 +5,7 @@ import AddBook from './AddBook.vue' import SignUp from './SignUp.vue' import LogIn from './LogIn.vue' import Home from './Home.vue' +import { useAuthStore } from './auth.store' const routes = [ { path: '/', component: Home }, @@ -18,3 +19,15 @@ 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'; + } +});