From d6b19e531ebab15f8cbce92dcb86e64c6da31031 Mon Sep 17 00:00:00 2001 From: Arthur Lefebvre Date: Thu, 9 Oct 2025 01:00:17 +0200 Subject: [PATCH] Redirect to login when not login --- front/src/router.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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'; + } +});