diff --git a/src/components/accounts/actions.js b/src/components/accounts/actions.js index e60c0e1..c2d065e 100644 --- a/src/components/accounts/actions.js +++ b/src/components/accounts/actions.js @@ -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: { diff --git a/tests/components/accounts/actions.test.js b/tests/components/accounts/actions.test.js index c532db5..6f8d274 100644 --- a/tests/components/accounts/actions.test.js +++ b/tests/components/accounts/actions.test.js @@ -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({