accounts-frontend/src/components/user/middlewares/bearerHeaderMiddleware.js

26 lines
561 B
JavaScript
Raw Normal View History

/**
* Applies Bearer header for all requests
*
* @param {object} store - redux store
* @param {function} store.getState
*
* @return {object} - request middleware
*/
export default function bearerHeaderMiddleware({getState}) {
return {
before(req) {
let {token} = getState().user;
if (req.options.token) {
token = req.options.token;
}
if (token) {
req.options.headers.Authorization = `Bearer ${token}`;
}
return req;
}
};
}