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..1c607a97 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 = () => {}) { let json = response.json(); if (response.status >= 200 && response.status < 300) { return json; } else { + cb() return json.then(err => Promise.reject(err)); } }