Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

Commit

Permalink
Clear client session on 401
Browse files Browse the repository at this point in the history
  • Loading branch information
wacii committed Nov 20, 2017
1 parent 7262004 commit b1cbc57
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion client/store/middleware/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import fetch from 'isomorphic-fetch'
import {normalize} from 'normalizr'
import {SubmissionError} from 'redux-form'

import {clearUser} from '../../modules/users/authReducer'

let _socket

const API_ROOT = process.env.NODE_ENV === 'production' ?
Expand Down Expand Up @@ -35,7 +37,7 @@ export function callApi(endpoint, method = 'GET', body, schema, responseSchema)
})
.then(response =>
response.json().then(json => {
if (!response.ok) return Promise.reject(json)
if (!response.ok) return Promise.reject({ ...json, status: response.status })
if (responseSchema) return normalize(json, responseSchema)
if (schema) return normalize(json, schema)
return json
Expand Down Expand Up @@ -93,6 +95,7 @@ export default socket => {
response
})),
error => {
if (error.status === 401) store.dispatch(clearUser())
next(actionWith({
type: failureType,
error
Expand Down
2 changes: 1 addition & 1 deletion client/store/middleware/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('api middleware', function() {
expect(fetchMock).to.calledWith('root/foo')

return result.catch(err => {
expect(err).to.eql({error: 'error'})
expect(err).to.include({error: 'error'})
expect(responseMock.json).to.have.been.calledOnce
})
})
Expand Down

0 comments on commit b1cbc57

Please sign in to comment.