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 SignUp from './SignUp.vue'
|
||||||
import LogIn from './LogIn.vue'
|
import LogIn from './LogIn.vue'
|
||||||
import Home from './Home.vue'
|
import Home from './Home.vue'
|
||||||
|
import { useAuthStore } from './auth.store'
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
{ path: '/', component: Home },
|
{ path: '/', component: Home },
|
||||||
@@ -18,3 +19,15 @@ export const router = createRouter({
|
|||||||
history: createWebHistory(),
|
history: createWebHistory(),
|
||||||
routes,
|
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