2016-08-08 00:47:58 +05:30
|
|
|
/**
|
|
|
|
* Applies Bearer header for all requests
|
|
|
|
*
|
2016-12-13 01:37:49 +05:30
|
|
|
* req.options.token is used to override current token.
|
|
|
|
* Pass null to disable bearer header at all
|
|
|
|
*
|
2016-08-08 00:47:58 +05:30
|
|
|
* @param {object} store - redux store
|
|
|
|
* @param {function} store.getState
|
|
|
|
*
|
|
|
|
* @return {object} - request middleware
|
|
|
|
*/
|
|
|
|
export default function bearerHeaderMiddleware({getState}) {
|
|
|
|
return {
|
2016-10-30 17:42:49 +05:30
|
|
|
before(req) {
|
2017-03-29 10:20:15 +05:30
|
|
|
const {accounts} = getState();
|
2016-11-05 15:41:41 +05:30
|
|
|
|
2017-03-29 10:20:15 +05:30
|
|
|
let {token} = accounts.active || {};
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2016-12-13 01:37:49 +05:30
|
|
|
if (req.options.token || req.options.token === null) {
|
2016-10-30 17:42:49 +05:30
|
|
|
token = req.options.token;
|
|
|
|
}
|
2016-08-08 00:47:58 +05:30
|
|
|
|
|
|
|
if (token) {
|
2016-10-30 17:42:49 +05:30
|
|
|
req.options.headers.Authorization = `Bearer ${token}`;
|
2016-08-08 00:47:58 +05:30
|
|
|
}
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
return req;
|
2016-08-08 00:47:58 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|