import {
createRouter,
createWebHistory
} from "vue-router";
import Home from "../views/Home.vue";
import PublicPage from "../views/public.vue";
const router = createRouter({
history: createWebHistory(),
routes: [{
path: "/public",
name: "PublicPage",
component: PublicPage,
meta: {
requiresAuth: false, // No authentication required
roles: [], // No specific roles required
},
path: "/",
name: "Home",
component: Home,
props: true,
meta: {
requiresAuth: true,
roles: ["Super Admin", "Admin", "Viewer"],
},
},
{
path: "/test",
component: () => import("../views/Test.vue"),
meta: {
requiresAuth: true,
roles: ["Super Admin", "Admin", "Viewer"],
},
},
{
path: "/map",
component: () => import("../views/Map.vue"),
meta: {
requiresAuth: true,
roles: ["Super Admin", "Admin", "Viewer"],
},
},
{
path: "/contact",
component: () => import("../views/Contact.vue"),
meta: {
requiresAuth: true,
roles: ["Super Admin", "Admin", "Viewer"],
},
},
{
path: "/users",
component: () => import("../views/Users.vue"),
meta: {
requiresAuth: true,
roles: ["Super Admin", "Admin"],
},
},
{
path: "/support",
component: () => import("../views/Support.vue"),
meta: {
requiresAuth: true,
roles: ["Super Admin", "Admin", "Viewer"],
},
},
{
path: "/settings",
component: () => import("../views/Settings.vue"),
meta: {
requiresAuth: true,
roles: ["Super Admin", "Admin"],
},
},
{
path: "/assets",
component: () => import("../views/Assets.vue"),
meta: {
requiresAuth: true,
roles: ["Super Admin", "Admin"],
},
},
{
path: "/alerts",
component: () => import("../views/Alerts.vue"),
meta: {
requiresAuth: true,
roles: ["Super Admin", "Admin"],
},
},
{
path: "/clients",
component: () => import("../views/Clients.vue"),
meta: {
requiresAuth: true,
roles: ["Super Admin", "Admin"], //only super admin have access to the company list
},
},
{
path: "/labels",
component: () => import("../views/Labels.vue"),
meta: {
requiresAuth: true,
roles: ["Super Admin", "Admin"],
},
},
{
path: "/login",
name: "Login",
component: () => import("../views/authentication/Login.vue"),
},
{
path: "/register",
name: "Register",
component: () => import("../views/authentication/Activate.vue"),
},
],
});
router.beforeEach((to, from, next) => {
const token = localStorage.getItem("token");
if (to.meta.requiresAuth && !token) {
next({
name: "Login"
});
} else if (token) {
const userRole = localStorage.getItem("role"); // implement this function to get the user's role from the backend
const authorizedRoles = to.meta.roles;
if (authorizedRoles && authorizedRoles.length && authorizedRoles.includes(userRole)) {
next();
} else {
next({
name: "Home"
}); // or redirect to a 403 page
}
} else {
next();
}
});
export default router;
Even if I am entering link as /public or /register which doesn’t need authentication it still redirects to the /login. It was working fine when i develope it first. Recently i updated my project by npm update. console.log(to.meta.requiresAuth) returns undefined and console.log(to.name) return login. There is no other place i have defined any redirect or rewrite rules for this project.