view client/src/router.js @ 955:920fba3f593f

chore: upgrading packages to latest
author Thomas Junk <thomas.junk@intevation.de>
date Tue, 16 Oct 2018 11:13:06 +0200
parents fb39ec3b95a8
children ca628dce90dd
line wrap: on
line source

import Vue from "vue";
import Router from "vue-router";
import store from "./store";
import {
  sessionStillActive,
  toMillisFromString
} from "./application/lib/session";

/*  facilitate codesplitting */
const Login = () => import("./login/Login.vue");
const Main = () => import("./application/Main.vue");
const Usermanagement = () => import("./usermanagement/Usermanagement.vue");
const Logs = () => import("./logs/logs.vue");
const Systemconfiguration = () =>
  import("./systemconfiguration/systemconfiguration.vue");

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/login",
      name: "login",
      component: Login
    },
    {
      path: "/usermanagement",
      name: "usermanagement",
      component: Usermanagement,
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        const isSysadmin = store.getters["user/isSysAdmin"];
        if (!isSysadmin) {
          next("/");
        } else {
          next();
        }
      }
    },
    {
      path: "/logs",
      name: "logs",
      component: Logs,
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        const isSysadmin = store.getters["user/isSysAdmin"];
        if (!isSysadmin) {
          next("/");
        } else {
          next();
        }
      }
    },
    {
      path: "/systemconfiguration",
      name: "systemconfiguration",
      component: Systemconfiguration,
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        const isSysadmin = store.getters["user/isSysAdmin"];
        if (!isSysadmin) {
          next("/");
        } else {
          next();
        }
      }
    },
    {
      path: "/",
      name: "mainview",
      component: Main,
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        const expiresFromPastSession = toMillisFromString(
          localStorage.getItem("expires")
        );
        if (sessionStillActive(expiresFromPastSession)) {
          store.commit("user/set_user", localStorage.getItem("user"));
          store.commit("user/set_expires", expiresFromPastSession);
          store.commit("user/set_roles", localStorage.getItem("roles"));
          store.commit("user/set_authenticate", true);
        } else {
          store.commit("user/clear_auth");
        }
        next();
      }
    },
    {
      path: "*",
      component: Login
    }
  ]
});

router.beforeEach((to, from, next) => {
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
  const loggedIn = store.getters["user/isAuthenticated"];
  const expiresFromPastSession = toMillisFromString(
    localStorage.getItem("expires")
  );
  const authRequired =
    requiresAuth && !(loggedIn || sessionStillActive(expiresFromPastSession));
  if (authRequired) {
    next("/login");
  } else if (!authRequired) {
    next();
  } else {
    next();
  }
});

export default router;