angularjs - NodeJs middleware set header -
i add add refresh token header in middleware seen in first answer of question: implementing refresh-tokens angular , 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 set header not received interceptor on client side. when debug res.header in npm console, set header 'authorization' in '_headers' instead of 'headers'.
how can solve this.
in express middleware, can set header using res.set(field, [value])
or res.header(field, [value])
(alias same function). setheader not express method, native node.js, assume might causing problem.
Comments
Post a Comment