changeset 1703:d0830ebb3a23

feat: when request answers with 401 the user is redirected to login
author Thomas Junk <thomas.junk@intevation.de>
date Mon, 07 Jan 2019 13:30:29 +0100
parents 49b89575ab31
children 897d4d8316ad
files client/src/components/importqueue/Importqueue.vue client/src/lib/http.js
diffstat 2 files changed, 18 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/components/importqueue/Importqueue.vue	Mon Jan 07 13:23:39 2019 +0100
+++ b/client/src/components/importqueue/Importqueue.vue	Mon Jan 07 13:30:29 2019 +0100
@@ -109,7 +109,6 @@
 import { displayError } from "@/lib/errors.js";
 import { mapState } from "vuex";
 import { HTTP } from "@/lib/http.js";
-import { logOff } from "@/lib/session.js";
 
 export default {
   name: "importqueue",
@@ -157,10 +156,6 @@
         })
         .catch(error => {
           const { status, data } = error.response;
-          if (status === 401) {
-            logOff();
-            return;
-          }
           displayError({
             title: this.$gettext("Backend Error"),
             message: `${status}: ${data.message || data}`
--- a/client/src/lib/http.js	Mon Jan 07 13:23:39 2019 +0100
+++ b/client/src/lib/http.js	Mon Jan 07 13:30:29 2019 +0100
@@ -12,11 +12,28 @@
  * Thomas Junk <thomas.junk@intevation.de>
  */
 
+import { logOff } from "@/lib/session.js";
+
 import axios from "axios";
 
-export const HTTP = axios.create({
+const UNAUTHORIZED = 401;
+
+const HTTP = axios.create({
   baseURL: process.env.VUE_APP_API_URL || "/api"
   /* headers: {
     Authorization: 'Bearer {token}'
   }*/
 });
+
+HTTP.interceptors.response.use(
+  response => response,
+  error => {
+    const { status } = error.response;
+    if (status === UNAUTHORIZED) {
+      logOff();
+    }
+    return Promise.reject(error);
+  }
+);
+
+export { HTTP };