Redirect to login when not login

This commit is contained in:
2025-10-09 01:00:17 +02:00
parent 2c6d0859f6
commit d6b19e531e

View File

@@ -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';
}
});