Skip to content
Snippets Groups Projects
Commit d11a848c authored by Titus Netland's avatar Titus Netland
Browse files

Merge branch 'vue-route-guard' into 'main'

Vue route guard

See merge request !22
parents 214b40f3 d4fe42f9
No related branches found
No related tags found
1 merge request!22Vue route guard
Pipeline #176532 passed
...@@ -4,34 +4,44 @@ import HomeView from "../views/HomeView.vue"; ...@@ -4,34 +4,44 @@ import HomeView from "../views/HomeView.vue";
import LoginView from "../views/LoginView.vue"; import LoginView from "../views/LoginView.vue";
import NewPasswordView from "../views/NewPasswordView"; import NewPasswordView from "../views/NewPasswordView";
/**
* Guards routes. If token is null, no user is logged in and only the
* login page and the register page will be accessible.
*/
function guardRoute(to, from, next) {
var isAuthenticated = false;
if (store.state.user.token != null) isAuthenticated = true;
else isAuthenticated = false;
if (isAuthenticated) {
next(); // allow to enter route
} else {
next("/login"); // go to '/login';
}
}
const routes = [ const routes = [
{ {
path: "/", //Endre før push path: "/", //Endre før push
name: "home", name: "home",
beforeEnter: guardRoute,
component: HomeView, component: HomeView,
}, },
{ {
path: "/about", path: "/about",
name: "about", name: "about",
beforeEnter: guardRoute,
component: () => import("../views/AboutView.vue"), component: () => import("../views/AboutView.vue"),
}, },
{ {
path: "/profile/:id", path: "/profile/:id",
name: "profile", name: "profile",
component: () => import("../views/ProfileView.vue"), component: () => import("../views/ProfileView.vue"),
beforeEnter: () => { beforeEnter: guardRoute,
if (store.state.user.token == null) router.push("login");
},
}, },
{ {
path: "/register", path: "/register",
name: "register", name: "register",
// route level code-splitting component: () => import("../views/RegisterView.vue"),
// this generates a separate chunk (register.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () =>
import(/* webpackChunkName: "register" */ "../views/RegisterView.vue"),
}, },
{ {
path: "/login", path: "/login",
...@@ -42,21 +52,25 @@ const routes = [ ...@@ -42,21 +52,25 @@ const routes = [
path: "/newPassword", path: "/newPassword",
name: "newPassword", name: "newPassword",
component: NewPasswordView, component: NewPasswordView,
beforeEnter: guardRoute,
}, },
{ {
path: "/searchItemList", path: "/searchItemList",
name: "searchItemList", name: "searchItemList",
component: () => import("../views/SearchItemListView.vue"), component: () => import("../views/SearchItemListView.vue"),
beforeEnter: guardRoute,
}, },
{ {
path: "/createNewGroup", path: "/createNewGroup",
name: "createNewGroup", name: "createNewGroup",
component: () => import("../views/CreateNewGroupView.vue"), component: () => import("../views/CreateNewGroupView.vue"),
beforeEnter: guardRoute,
}, },
{ {
path: "/addNewItem", path: "/addNewItem",
name: "addNewItem", name: "addNewItem",
component: () => import("../views/AddNewItemView.vue"), component: () => import("../views/AddNewItemView.vue"),
beforeEnter: guardRoute,
}, },
{ {
path: "/notifications", path: "/notifications",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment