From 0bfa8216e7e9fba31095047ec62750398d3cf5cb Mon Sep 17 00:00:00 2001 From: Alberto Harres Rocha Date: Mon, 19 Sep 2016 12:29:21 -0300 Subject: [PATCH 1/2] Fix clientOnly parsing undefined and fix modals After getting the initialCredentials, the lib was trying to persist data using undefined variables and returning a undefined user. Also, the variables `firstTimeLogin` and `mustResetPassword` were never set in the clientOnly mode, so the actionsCreator related to them was never being fired due the conditional statement requiring these variables to be set. The parseResponse method was refactored to allow a optional callback in order to be reused in the applyConfig method as well. --- src/actions/configure.js | 4 ++++ src/utils/client-settings.js | 18 +++++++++++------- src/utils/handle-fetch-response.js | 3 ++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/actions/configure.js b/src/actions/configure.js index 9905182a..79217f8d 100644 --- a/src/actions/configure.js +++ b/src/actions/configure.js @@ -106,6 +106,10 @@ export function configure(endpoint={}, settings={}) { let {authRedirectPath, authRedirectHeaders} = getRedirectInfo(window.location); + mustResetPassword = authRedirectHeaders.reset_password ? true : false + firstTimeLogin = authRedirectHeaders + .account_confirmation_success ? true : false + if (authRedirectPath) { dispatch(push({pathname: authRedirectPath})); } diff --git a/src/utils/client-settings.js b/src/utils/client-settings.js index 7efa569e..5937444d 100644 --- a/src/utils/client-settings.js +++ b/src/utils/client-settings.js @@ -2,6 +2,7 @@ import * as C from "./constants"; import extend from "extend"; import fetch from "./fetch"; import parseEndpointConfig from "./parse-endpoint-config"; +import {parseResponse} from "./handle-fetch-response"; import {setEndpointKeys} from "../actions/configure"; import { getApiUrl, @@ -95,18 +96,21 @@ export function applyConfig({dispatch, endpoint={}, settings={}, reset=false}={} if (getCurrentSettings().initialCredentials) { // skip initial headers check (i.e. check was already done server-side) - let {user, headers} = getCurrentSettings().initialCredentials; + let headers = getCurrentSettings().initialCredentials; persistData(C.SAVED_CREDS_KEY, headers); - return Promise.resolve(user); + return fetch(`${getApiUrl(currentEndpointKey)}${currentEndpoint[currentEndpointKey].tokenValidationPath}`) + .then(response => { + return parseResponse(response, + () => (removeData(C.SAVED_CREDS_KEY))) + .then(({data}) => (data)); + }); } else if (savedCreds) { // verify session credentials with API return fetch(`${getApiUrl(currentEndpointKey)}${currentEndpoint[currentEndpointKey].tokenValidationPath}`) .then(response => { - if (response.status >= 200 && response.status < 300) { - return response.json().then(({ data }) => (data)); - } - removeData(C.SAVED_CREDS_KEY); - return Promise.reject({reason: "No credentials."}); + return parseResponse(response, + () => (removeData(C.SAVED_CREDS_KEY))) + .then(({data}) => (data)); }); } else { return Promise.reject({reason: "No credentials."}) diff --git a/src/utils/handle-fetch-response.js b/src/utils/handle-fetch-response.js index 2280da71..58692ce3 100644 --- a/src/utils/handle-fetch-response.js +++ b/src/utils/handle-fetch-response.js @@ -1,8 +1,9 @@ -export function parseResponse (response) { +export function parseResponse (response, cb = function(){}) { let json = response.json(); if (response.status >= 200 && response.status < 300) { return json; } else { + cb() return json.then(err => Promise.reject(err)); } } From 8d24d5580b7be822b927bbd2a2f6723264483e50 Mon Sep 17 00:00:00 2001 From: Alberto Harres Rocha Date: Mon, 19 Sep 2016 12:47:55 -0300 Subject: [PATCH 2/2] Refactor method calling to ES6 --- src/utils/handle-fetch-response.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/handle-fetch-response.js b/src/utils/handle-fetch-response.js index 58692ce3..1c607a97 100644 --- a/src/utils/handle-fetch-response.js +++ b/src/utils/handle-fetch-response.js @@ -1,4 +1,4 @@ -export function parseResponse (response, cb = function(){}) { +export function parseResponse (response, cb = () => {}) { let json = response.json(); if (response.status >= 200 && response.status < 300) { return json;