Skip to content

Commit

Permalink
fix: gracefully handle empty APIs block
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Dec 3, 2018
1 parent 732005c commit 94bd5f9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ component-map.json
stats.json
jest*
workspace/

test/
stories/
scripts/
docs/
8 changes: 4 additions & 4 deletions app/lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ Router.prototype.webRoutes = function () {
)
let accessToken = req.cookies && req.cookies.accessToken
let authenticate = Promise.resolve()
let apis = config.get('apis')
let mainApi = apis.length && apis[0]

if (accessToken) {
let api = config.get('apis.0')

if (accessToken && mainApi) {
authenticate = request({
headers: {
Authorization: `Bearer ${accessToken}`
},
json: true,
uri: `${api.host}:${api.port}/api/client`
uri: `${mainApi.host}:${mainApi.port}/api/client`
}).then(({results}) => {
entryPointPage = entryPointPage
.replace(
Expand Down
7 changes: 4 additions & 3 deletions app/lib/router/routes/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@ module.exports = function (app) {
if (!app) return

app.get('/config', (req, res, next) => {
let apis = config.get('apis')
let mainApi = apis.length && apis[0]
let emptyResponse = {
config: null,
routes: null
}
let accessToken = (req.cookies && req.cookies.accessToken) ||
req.query.accessToken

if (!accessToken) {
if (!accessToken || !mainApi) {
return res.end(
JSON.stringify(emptyResponse)
)
}

let api = config.get('apis.0')
let requestOptions = {
headers: {
Authorization: `Bearer ${accessToken}`
},
json: true,
uri: `${api.host}:${api.port}/api/client`
uri: `${mainApi.host}:${mainApi.port}/api/client`
}

return request(requestOptions).then(({results}) => {
Expand Down
66 changes: 36 additions & 30 deletions frontend/reducers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,35 @@ function mergeUpdate (current, fieldName, value) {
}
}

function signOut () {
Cookies.remove('accessToken')
Cookies.remove('accessTokenExpiry')

return {
...initialState,
accessToken: undefined,
isSaving: false,
isSignedIn: false,
sessionHasExpired: Boolean(action.sessionHasExpired)
}
}

export default function user (state = initialState, action = {}) {
switch (action.type) {

case Types.SET_API_LIST:
if (action.apis.length === 0) {
return signOut()
}

return state

case Types.ATTEMPT_SAVE_USER:
return {
...state,
hasBeenSubmitted: true
}

// Action: authenticate
case Types.AUTHENTICATE:
let {
accessToken,
Expand Down Expand Up @@ -89,6 +108,21 @@ export default function user (state = initialState, action = {}) {
sessionHasExpired: false
}

case Types.REGISTER_NETWORK_ERROR:
// We're interested in processing errors with the code API-0005 only, as
// those signal an authentication error (i.e. the access token stored is
// no longer valid). If that's not the error we're seeing here, there's
// nothing for us to do. If it is, we return the result of `signOut()`.
if (
!action.error ||
!action.error.code ||
action.error.code !== Constants.API_UNAUTHORISED_ERROR
) {
return state
}

return signOut()

case Types.SET_API_STATUS:
if (action.error === Constants.API_UNAUTHORISED_ERROR) {
Cookies.remove('accessToken')
Expand All @@ -114,7 +148,6 @@ export default function user (state = initialState, action = {}) {
remoteError: null
}

// Document action: set field error status
case Types.SET_USER_FIELD_ERROR_STATUS:
const {
error = null,
Expand All @@ -141,7 +174,6 @@ export default function user (state = initialState, action = {}) {
}
}

// Action: set user status
case Types.SET_USER_STATUS:
switch (action.status) {

Expand Down Expand Up @@ -174,35 +206,9 @@ export default function user (state = initialState, action = {}) {

return state

// Action: processing error from the network
case Types.REGISTER_NETWORK_ERROR:
// We're interested in processing errors with the code API-0005 only, as
// those signal an authentication error (i.e. the access token stored is
// no longer valid). If that's not the error we're seeing here, there's
// nothing for us to do. If it is, we let the switch go to the next case,
// where the sign out routine is processed.
if (
!action.error ||
!action.error.code ||
action.error.code !== Constants.API_UNAUTHORISED_ERROR
) {
return state
}

// Action: clear user
case Types.SIGN_OUT:
Cookies.remove('accessToken')
Cookies.remove('accessTokenExpiry')

return {
...initialState,
accessToken: undefined,
isSaving: false,
isSignedIn: false,
sessionHasExpired: Boolean(action.sessionHasExpired)
}
return signOut()

// Action: update local user
case Types.UPDATE_LOCAL_USER:
return mergeUpdate(
{...state, hasBeenValidated: true},
Expand Down

0 comments on commit 94bd5f9

Please sign in to comment.