Redirect to login when not login
This commit is contained in:
@@ -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';
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user