Sfoglia il codice sorgente

Fix redirection fail on request with status 401

The app would not redirect to the login page, if a request fails with
401. Reproducible on production build only.

Cause by removing the '#' from URL feature, since `window.location.hash`
is no longer available on production.
Sergiu Miclea 7 anni fa
parent
commit
75016a09eb
1 ha cambiato i file con 10 aggiunte e 6 eliminazioni
  1. 10 6
      src/utils/ApiCaller.js

+ 10 - 6
src/utils/ApiCaller.js

@@ -20,7 +20,6 @@ import cookie from 'js-cookie'
 
 
 import logger from './ApiLogger'
 import logger from './ApiLogger'
 import notificationStore from '../stores/NotificationStore'
 import notificationStore from '../stores/NotificationStore'
-import DomUtils from '../utils/DomUtils'
 
 
 type Cancelable = {
 type Cancelable = {
   requestId: string,
   requestId: string,
@@ -48,6 +47,13 @@ const addCancelable = (cancelable: Cancelable) => {
   }
   }
 }
 }
 
 
+const isOnLoginPage = (): boolean => {
+  if (window.env.ENV === 'development') {
+    return window.location.hash === '#/' || window.location.hash === '#/login'
+  }
+  return window.location.pathname === '/' || window.location.pathname === '/login'
+}
+
 class ApiCaller {
 class ApiCaller {
   constructor() {
   constructor() {
     axios.defaults.headers.common['Content-Type'] = 'application/json'
     axios.defaults.headers.common['Content-Type'] = 'application/json'
@@ -107,13 +113,11 @@ class ApiCaller {
         }
         }
         resolve(response)
         resolve(response)
       }).catch(error => {
       }).catch(error => {
-        const loginUrl = `${DomUtils.urlHashPrefix}`
-
         if (error.response) {
         if (error.response) {
           // The request was made and the server responded with a status code
           // The request was made and the server responded with a status code
           // that falls out of the range of 2xx
           // that falls out of the range of 2xx
           if (
           if (
-            (error.response.status !== 401 || window.location.hash !== loginUrl) &&
+            (error.response.status !== 401 || !isOnLoginPage()) &&
             !options.quietError &&
             !options.quietError &&
             error.response.data) {
             error.response.data) {
             let data = error.response.data
             let data = error.response.data
@@ -123,7 +127,7 @@ class ApiCaller {
             }
             }
           }
           }
 
 
-          if (error.response.status === 401 && window.location.hash !== loginUrl && error.request.responseURL.indexOf('/proxy/') === -1) {
+          if (error.response.status === 401 && !isOnLoginPage() && error.request.responseURL.indexOf('/proxy/') === -1) {
             window.location.href = '/'
             window.location.href = '/'
           }
           }
 
 
@@ -138,7 +142,7 @@ class ApiCaller {
         } else if (error.request) {
         } else if (error.request) {
           // The request was made but no response was received
           // The request was made but no response was received
           // `error.request` is an instance of XMLHttpRequest
           // `error.request` is an instance of XMLHttpRequest
-          if (window.location.hash !== loginUrl) {
+          if (!isOnLoginPage()) {
             notificationStore.alert('Request failed, there might be a problem with the connection to the server.', 'error')
             notificationStore.alert('Request failed, there might be a problem with the connection to the server.', 'error')
           }
           }
           logger.log({
           logger.log({