#303: do not logout user if 5xx during authentication

This commit is contained in:
SleepWalker 2017-02-24 07:50:32 +02:00
parent adfc95d93d
commit 4074a65329
2 changed files with 29 additions and 2 deletions

View File

@ -35,13 +35,18 @@ export { updateToken };
export function authenticate({token, refreshToken}) {
return (dispatch, getState) =>
authentication.validateToken({token, refreshToken})
.catch((resp) => {
.catch((resp = {}) => {
if (resp.originalResponse && resp.originalResponse.status >= 500) {
// delegate error recovering to the later logic
return Promise.reject(resp);
}
logger.warn('Error validating token during auth', {
resp
});
return dispatch(logoutAll())
.then(() => Promise.reject());
.then(() => Promise.reject(resp));
})
.then(({token, refreshToken, user}) => ({
user: {

View File

@ -3,6 +3,7 @@ import sinon from 'sinon';
import { routeActions } from 'react-router-redux';
import logger from 'services/logger';
import authentication from 'services/api/authentication';
import {
authenticate,
@ -55,6 +56,7 @@ describe('components/accounts/actions', () => {
});
sinon.stub(authentication, 'validateToken').named('authentication.validateToken');
sinon.stub(logger, 'warn').named('logger.warn');
authentication.validateToken.returns(Promise.resolve({
token: account.token,
refreshToken: account.refreshToken,
@ -64,6 +66,7 @@ describe('components/accounts/actions', () => {
afterEach(() => {
authentication.validateToken.restore();
logger.warn.restore();
});
describe('#authenticate()', () => {
@ -117,6 +120,9 @@ describe('components/accounts/actions', () => {
authentication.validateToken.returns(Promise.reject({}));
return expect(authenticate(account)(dispatch, getState), 'to be rejected').then(() => {
expect(logger.warn, 'to have a call satisfying', [
'Error validating token during auth', {}
]);
expect(dispatch, 'to have a call satisfying', [
{payload: {isGuest: true}},
]);
@ -126,6 +132,22 @@ describe('components/accounts/actions', () => {
});
});
it('rejects when 5xx without logouting', () => {
const resp = {
originalResponse: {status: 500}
};
authentication.validateToken.returns(Promise.reject(resp));
return expect(authenticate(account)(dispatch, getState), 'to be rejected with', resp).then(() => {
expect(dispatch, 'to have no calls satisfying', [
{payload: {isGuest: true}},
]);
expect(logger.warn, 'was not called');
});
});
it('marks user as stranger, if there is no refreshToken', () => {
const expectedKey = `stranger${account.id}`;
authentication.validateToken.returns(Promise.resolve({