From 91982348ee73385db9b830571cef5bde461063de Mon Sep 17 00:00:00 2001 From: Joe Turner Date: Mon, 26 Mar 2018 15:10:29 -0400 Subject: [PATCH] Don't deal with standard express errors any more Made possible by https://github.com/Thinkful-Ed/node-jwt-auth/pull/8/files --- src/actions/auth.js | 62 ++++++++++++++++------------------- src/actions/protected-data.js | 4 +-- src/actions/users.js | 4 +-- src/actions/utils.js | 23 +++---------- 4 files changed, 38 insertions(+), 55 deletions(-) diff --git a/src/actions/auth.js b/src/actions/auth.js index 3b4d6cb..c7ee6ff 100644 --- a/src/actions/auth.js +++ b/src/actions/auth.js @@ -2,7 +2,7 @@ import jwtDecode from 'jwt-decode'; import {SubmissionError} from 'redux-form'; import {API_BASE_URL} from '../config'; -import {normalizeResponseErrors} from './utils'; +import {handleResponseErrors} from './utils'; import {saveAuthToken, clearAuthToken} from '../local-storage'; export const SET_AUTH_TOKEN = 'SET_AUTH_TOKEN'; @@ -44,38 +44,34 @@ const storeAuthInfo = (authToken, dispatch) => { export const login = (username, password) => dispatch => { dispatch(authRequest()); - return ( - fetch(`${API_BASE_URL}/auth/login`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - username, - password - }) + return fetch(`${API_BASE_URL}/auth/login`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + username, + password }) - // Reject any requests which don't return a 200 status, creating - // errors which follow a consistent format - .then(res => normalizeResponseErrors(res)) - .then(res => res.json()) - .then(({authToken}) => storeAuthInfo(authToken, dispatch)) - .catch(err => { - const {code} = err; - const message = - code === 401 - ? 'Incorrect username or password' - : 'Unable to login, please try again'; - dispatch(authError(err)); - // Could not authenticate, so return a SubmissionError for Redux - // Form - return Promise.reject( - new SubmissionError({ - _error: message - }) - ); - }) - ); + }) + .then(res => handleResponseErrors(res)) + .then(res => res.json()) + .then(({authToken}) => storeAuthInfo(authToken, dispatch)) + .catch(err => { + const {status} = err; + const message = + status === 401 + ? 'Incorrect username or password' + : 'Unable to login, please try again'; + dispatch(authError(err)); + // Could not authenticate, so return a SubmissionError for Redux + // Form + return Promise.reject( + new SubmissionError({ + _error: message + }) + ); + }); }; export const refreshAuthToken = () => (dispatch, getState) => { @@ -88,7 +84,7 @@ export const refreshAuthToken = () => (dispatch, getState) => { Authorization: `Bearer ${authToken}` } }) - .then(res => normalizeResponseErrors(res)) + .then(res => handleResponseErrors(res)) .then(res => res.json()) .then(({authToken}) => storeAuthInfo(authToken, dispatch)) .catch(err => { diff --git a/src/actions/protected-data.js b/src/actions/protected-data.js index a1c9e08..37612a6 100644 --- a/src/actions/protected-data.js +++ b/src/actions/protected-data.js @@ -1,5 +1,5 @@ import {API_BASE_URL} from '../config'; -import {normalizeResponseErrors} from './utils'; +import {handleResponseErrors} from './utils'; export const FETCH_PROTECTED_DATA_SUCCESS = 'FETCH_PROTECTED_DATA_SUCCESS'; export const fetchProtectedDataSuccess = data => ({ @@ -22,7 +22,7 @@ export const fetchProtectedData = () => (dispatch, getState) => { Authorization: `Bearer ${authToken}` } }) - .then(res => normalizeResponseErrors(res)) + .then(res => handleResponseErrors(res)) .then(res => res.json()) .then(({data}) => dispatch(fetchProtectedDataSuccess(data))) .catch(err => { diff --git a/src/actions/users.js b/src/actions/users.js index 6b336da..35c9dbf 100644 --- a/src/actions/users.js +++ b/src/actions/users.js @@ -1,7 +1,7 @@ import {SubmissionError} from 'redux-form'; import {API_BASE_URL} from '../config'; -import {normalizeResponseErrors} from './utils'; +import {handleResponseErrors} from './utils'; export const registerUser = user => dispatch => { return fetch(`${API_BASE_URL}/users`, { @@ -11,7 +11,7 @@ export const registerUser = user => dispatch => { }, body: JSON.stringify(user) }) - .then(res => normalizeResponseErrors(res)) + .then(res => handleResponseErrors(res)) .then(res => res.json()) .catch(err => { const {reason, message, location} = err; diff --git a/src/actions/utils.js b/src/actions/utils.js index c245433..7c21627 100644 --- a/src/actions/utils.js +++ b/src/actions/utils.js @@ -1,22 +1,9 @@ -// Boilerplate code for handling errors from the API. If the error response -// contains JSON then we return a rejected promise containing the decoded -// JSON. If the error doesn't contain JSON then we return a rejected promise -// containing the status text. If there is no error then we continue with -// the promise chain. -export const normalizeResponseErrors = res => { +// Boilerplate code for handling errors from the API. If the API sent back an +// error, return a rejected promise containing the decoded JSON body. If the +// request succeeded then we continue with the promise chain. +export const handleResponseErrors = res => { if (!res.ok) { - if ( - res.headers.has('content-type') && - res.headers.get('content-type').startsWith('application/json') - ) { - // It's a nice JSON error returned by us, so decode it - return res.json().then(err => Promise.reject(err)); - } - // It's a less informative error returned by express - return Promise.reject({ - code: res.status, - message: res.statusText - }); + return res.json().then(err => Promise.reject(err)); } return res; };