Skip to content

Commit

Permalink
Do redirect to login when error is authentication error
Browse files Browse the repository at this point in the history
  • Loading branch information
javierbrea committed Feb 13, 2019
1 parent b307389 commit cd98b11
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/data-sources/login.js
Expand Up @@ -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];
Expand All @@ -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 {
Expand Down

0 comments on commit cd98b11

Please sign in to comment.