I would like to add to add a refresh token to a header in middleware as seen in the first answer of this question: implementing refresh-tokens with angular and express-jwt
Backend - middleware:
jwt.verify(token, config.secret, function(err, decoded) {
if (err) {
res.status(401);
return res.json({ success: false, message: 'Authentication failed' });
} else {
var token_exp = new Date(decoded.exp * 1000);
var date = new Date();
var difference = (token_exp.getTime() - date.getTime()) / 60000;
if(difference < (config.expireTimeToken / 2 )){
var expires = config.expireTimeToken + 'm';
var token = jwt.refreshToken(decoded);
res.setHeader('Authorization', 'Bearer ' + token);
next();
}else{
req.decoded = decoded;
next();
}
}
});
Front-end interceptor:
module.exports = function ($injector) {
return {
request: function (config) {
var CoreService = $injector.get('CoreService');
config.headers['x-access-token'] = CoreService.getToken();
return config;
},
response: function(response) {
var receivedToken = response.headers('authorization');
console.log(receivedToken);
return response;
}
};
};
However, whenever i set a header it is not received by the interceptor on the client side. When i debug the res.header in my npm console, the set header 'Authorization' is in '_headers' instead of 'headers'.
How can i solve this.