changeset 2846:b42d10120cad

client: fixed router redirects when not logged in
author Markus Kottlaender <markus@intevation.de>
date Thu, 28 Mar 2019 14:07:20 +0100
parents 7cd708b98239
children 4da07a11e6e1
files client/src/components/Login.vue client/src/router.js
diffstat 2 files changed, 18 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/components/Login.vue	Thu Mar 28 12:18:40 2019 +0100
+++ b/client/src/components/Login.vue	Thu Mar 28 14:07:20 2019 +0100
@@ -1,4 +1,4 @@
-(<template>
+<template>
   <div class="d-flex flex-column login bg-white shadow">
     <div class="m-5">
       <!-- logo section -->
@@ -84,8 +84,8 @@
         <img :src="secondaryLogo" />
       </div>
     </div>
-  </div> </template
->)
+  </div>
+</template>
 
 <style lang="scss" scoped>
 .login {
@@ -193,7 +193,8 @@
         .dispatch("user/login", { user, password })
         .then(() => {
           this.loginFailed = false;
-          this.$router.push("/");
+          this.$router.push(localStorage.getItem("tempRoute") || "/");
+          localStorage.removeItem("tempRoute");
         })
         .catch(error => {
           this.loginFailed = true;
--- a/client/src/router.js	Thu Mar 28 12:18:40 2019 +0100
+++ b/client/src/router.js	Thu Mar 28 14:07:20 2019 +0100
@@ -41,7 +41,7 @@
       beforeEnter: (to, from, next) => {
         const isSysadmin = store.getters["user/isSysAdmin"];
         if (!isSysadmin) {
-          next("/");
+          next("/login");
         } else {
           next();
         }
@@ -57,7 +57,7 @@
       beforeEnter: (to, from, next) => {
         const isSysadmin = store.getters["user/isSysAdmin"];
         if (!isSysadmin) {
-          next("/");
+          next("/login");
         } else {
           next();
         }
@@ -74,7 +74,7 @@
       beforeEnter: (to, from, next) => {
         const isWaterwayAdmin = store.getters["user/isWaterwayAdmin"];
         if (!isWaterwayAdmin) {
-          next("/");
+          next("/login");
         } else {
           next();
         }
@@ -90,7 +90,7 @@
       beforeEnter: (to, from, next) => {
         const isWaterwayAdmin = store.getters["user/isWaterwayAdmin"];
         if (!isWaterwayAdmin) {
-          next("/");
+          next("/login");
         } else {
           next();
         }
@@ -106,7 +106,7 @@
       beforeEnter: (to, from, next) => {
         const isWaterwayAdmin = store.getters["user/isWaterwayAdmin"];
         if (!isWaterwayAdmin) {
-          next("/");
+          next("/login");
         } else {
           next();
         }
@@ -122,7 +122,7 @@
       beforeEnter: (to, from, next) => {
         const isWaterwayAdmin = store.getters["user/isWaterwayAdmin"];
         if (!isWaterwayAdmin) {
-          next("/");
+          next("/login");
         } else {
           next();
         }
@@ -138,7 +138,7 @@
       beforeEnter: (to, from, next) => {
         const isWaterwayAdmin = store.getters["user/isWaterwayAdmin"];
         if (!isWaterwayAdmin) {
-          next("/");
+          next("/login");
         } else {
           next();
         }
@@ -184,7 +184,7 @@
       beforeEnter: (to, from, next) => {
         const isWaterwayAdmin = store.getters["user/isWaterwayAdmin"];
         if (!isWaterwayAdmin) {
-          next("/");
+          next("/login");
         } else {
           store.commit("application/showContextBox", true);
           store.commit("application/contextBoxContent", "importoverview");
@@ -203,7 +203,7 @@
       beforeEnter: (to, from, next) => {
         const isSysadmin = store.getters["user/isSysAdmin"];
         if (!isSysadmin) {
-          next("/");
+          next("/login");
         } else {
           store.commit("application/searchQuery", "");
           store.commit("application/showContextBox", true);
@@ -233,24 +233,13 @@
     store.commit("reset");
     store.commit("user/clearAuth");
   }
-  const { tempRoute } = store.state.application;
   const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
-  const loggedIn = store.state.user.isAuthenticated;
-  const authRequired =
-    requiresAuth && !(loggedIn || sessionStillActive(expiresFromPastSession));
-  if (authRequired) {
-    store.commit("application/setTempRoute", to.path);
+  const redirectToLogin = requiresAuth && !store.state.user.isAuthenticated;
+  if (redirectToLogin) {
+    localStorage.setItem("tempRoute", to.path);
     next("/login");
-  } else {
-    if (tempRoute) {
-      const target = tempRoute;
-      store.commit("application/setTempRoute", "");
-      next({
-        path: target,
-        replace: true
-      });
-    } else next();
   }
+  next();
 });
 
 export default router;