From cd98b11cf7a755c1333643c830db18e64e9e0dc8 Mon Sep 17 00:00:00 2001 From: Javier Brea Date: Wed, 13 Feb 2019 20:03:28 +0100 Subject: [PATCH] Do redirect to login when error is authentication error --- src/data-sources/login.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/data-sources/login.js b/src/data-sources/login.js index 9ce7d94..f73be07 100644 --- a/src/data-sources/login.js +++ b/src/data-sources/login.js @@ -11,6 +11,7 @@ class Login { } _doLogin(dataSources, retry) { + const noAuthenticationTokenError = new Error("No authentication token found"); return Promise.all([this._refreshToken.read(), this._apiKey.read()]) .then(tokens => { const refreshToken = tokens[0]; @@ -28,16 +29,18 @@ class Login { setApiKey(apiKey); return retry(); } - return Promise.reject(new Error("No authentication token found")); + return Promise.reject(noAuthenticationTokenError); }) .catch(error => { - if (error.response && error.response.status === 401) { + if (error === noAuthenticationTokenError || error.message === "Unauthorized") { return Promise.all([this._refreshToken.delete(), this._apiKey.delete()]).then(() => { - this._history.push( - `${this._loginRoute}?${queryString.stringify({ - redirect: this._history.location.pathname - })}` - ); + const previousLocation = + this._history.location.pathname !== this._loginRoute + ? `?${queryString.stringify({ + redirect: this._history.location.pathname + })}` + : ""; + this._history.push(`${this._loginRoute}${previousLocation}`); return Promise.reject(error); }); } else {