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";
import LoginView from "../views/LoginView.vue";
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 = [
{
path: "/", //Endre før push
name: "home",
beforeEnter: guardRoute,
component: HomeView,
},
{
path: "/about",
name: "about",
beforeEnter: guardRoute,
component: () => import("../views/AboutView.vue"),
},
{
path: "/profile/:id",
name: "profile",
component: () => import("../views/ProfileView.vue"),
beforeEnter: () => {
if (store.state.user.token == null) router.push("login");
},
beforeEnter: guardRoute,
},
{
path: "/register",
name: "register",
// route level code-splitting
// 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"),
component: () => import("../views/RegisterView.vue"),
},
{
path: "/login",
......@@ -42,21 +52,25 @@ const routes = [
path: "/newPassword",
name: "newPassword",
component: NewPasswordView,
beforeEnter: guardRoute,
},
{
path: "/searchItemList",
name: "searchItemList",
component: () => import("../views/SearchItemListView.vue"),
beforeEnter: guardRoute,
},
{
path: "/createNewGroup",
name: "createNewGroup",
component: () => import("../views/CreateNewGroupView.vue"),
beforeEnter: guardRoute,
},
{
path: "/addNewItem",
name: "addNewItem",
component: () => import("../views/AddNewItemView.vue"),
beforeEnter: guardRoute,
},
{
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