3

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.

Community
  • 1
  • 1
pmulders
  • 89
  • 5

1 Answers1

0

In an express middleware, you can set the header using res.set(field, [value]) or res.header(field, [value]) (alias for the same function). setHeader is not the express method, but the native node.js, I assume this might be causing the problem.

marton
  • 1,300
  • 8
  • 16
  • I have tried set, header and setHeader with various combinations, but none of these have solved my problem. – pmulders Jul 06 '16 at 12:16