Unauthorized Responses
Some applications log users out after a certain period of time. For single page apps, this usually presents itself as requests to the backend suddenly failing for the logged-out user. This can occur when the user leaves the application open in their browser over night, for instance.
A common UX pattern is to display a notification to the user when this occurs, so that they can log back in. This recipe describes one way that you can detect when a user is logged out, so that you can notify them however you see fit.

Your server

The backend that you interface with needs to provide a consistent representation of the user being logged out.
If your API uses proper HTTP status codes, then this means that the responses will have a 401 status code.
The rest of this guide will assume that the backend returns a 401 HTTP status code, although this same system works for other representations, too.

Action Creators

Whenever an unauthorized response from the backend is returned in your CRUD action creators, include the status code in the request action that you dispatch.
You can attach arbitrary data to a request by specifying requestProperties on the action.
If you're using Redux Resource XHR, then this will be set for you. You can access the status code at action.requestProperties.statusCode.
If you're not using Redux Resource XHR, then your code may look something like:
1
export function readBook(id) {
2
return function(dispatch) {
3
dispatch({
4
type: actionTypes.READ_RESOURCES_PENDING,
5
resourceType: 'books',
6
resources: [id]
7
});
8
9
// `request` would be whatever function you are using to make HTTP
10
// requests. It could be `window.fetch()`, axios, superagent, xhr,
11
// or anything else that you prefer.
12
request('/some-url', (err, res) => {
13
// Different libraries attach the status code to different properties.
14
// It is usually either `res.status` or `res.statusCode`. You do not
15
// need to check both properties if you know which one your library uses.
16
if (err || res.statusCode >= 400 || res.status >= 400) {
17
dispatch({
18
type: actionTypes.READ_RESOURCES_FAILED,
19
resourceType: 'books',
20
resources: [id],
21
requestProperties: {
22
statusCode: res.statusCode
23
}
24
});
25
26
return;
27
}
28
29
// Check to see if the request was cancelled, or if it succeeded, then
30
// dispatch the appropriate action here.
31
});
32
}
33
}
Copied!

Reducer

The final step is to write a reducer that updates the state tree whenever these failed actions occur. Here is an example reducer that does this:
1
import { actionTypes } from 'redux-resource';
2
3
const {
4
READ_RESOURCES_FAILED,
5
UPDATE_RESOURCES_FAILED,
6
CREATE_RESOURCES_FAILED,
7
DELETE_RESOURCES_FAILED
8
} = actionTypes;
9
10
export default function reducer(state = false, action) {
11
const isFailedAction = action.type === READ_RESOURCES_FAILED ||
12
action.type === UPDATE_RESOURCES_FAILED ||
13
action.type === CREATE_RESOURCES_FAILED ||
14
action.type === DELETE_RESOURCES_FAILED;
15
16
if (isFailedAction && action.requestProperties.statusCode === 401) {
17
return true;
18
}
19
20
return state;
21
}
Copied!
Before an unauthorized response has been received, this reducer will return a state of false. When a logged-out response is received, then the reducer will update the state to be true.
You'll want to use this reducer with combineReducers:
1
import { combineReducers } from 'redux';
2
import unauthorizedReducer from './unauthorized-reducer';
3
4
const reducer = combineReducers({
5
unauthorized: unauthorizedReducer,
6
// ...other store slices (your resource reducers, etc.)
7
});
Copied!
With this in place, you have a slice of your state tree that will be true whenever an unauthorized response is received.
You can use this value in your view layer to toggle the appearance of a modal, or some other notification, to let the user know that they need to log back in.