-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Closed
Labels
need reproReproduction code is requiredReproduction code is required
Description
What problem does this feature solve?
I want to set a guard in VueRouter.beforeEach(), it will redirect to the login page when the loggedIn
state is false
.
What does the proposed API look like?
I want to achieve something this this:
import { RouteRecordRaw, createRouter, createWebHistory } from "vue-router";
import store from "@/store";
const routes: Array<RouteRecordRaw> = [
{
path: "/login",
name: "Login",
component: () => import("../views/Login.vue")
},
{
path: "/",
name: "Home",
component: () => import("../views/Home.vue"),
meta: {
requiresAuth: true
}
},
];
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
});
router.beforeEach((to, from, next) => {
const loggedIn = store.state.authentication.loggedIn;
if (to.matched.some(record => record.meta.requiresAuth) && !loggedIn) {
next({
name: "Login",
query: { redirect: to.fullPath }
});
} else {
next();
}
});
export default router;
vane-llope
Metadata
Metadata
Assignees
Labels
need reproReproduction code is requiredReproduction code is required