No authorization token was found when res.redirect

Viewed 5217

I have two applications, both on Nodejs. One front-end and other back-end. My back-end app is protected with token access using express-jwt and jsonwebtoken middlewares.

My problem is: I am making a request from front-end to back-end passing the token on header, back-end accepts the request and respond properly. Then in the front-end I redirect the response to an specific page (res.redirect('/')), in that moment I get the error UnauthorizedError: No authorization token was found

My front-end request:

/* Authentication */
router.post('/', function(req, res, next) {

    // request login service
    request({
        uri: env.getUrl() + "/user",
        method: 'POST',
        timeout: 10000,
        headers: {
            'Authorization': 'Bearer '.concat(global.token)
        },
        form: { login : req.body.login, pwd : req.body.pwd }
    }, function(error, response, body){
        if(error) {
            logger.error(error);
            res.render("error", {message: "Error getting user" }); 
        }
        else {
            if(body){
                req.session.usuario = JSON.parse(body);
                res.redirect("/");
            } else {
                res.render("login", {message: "Login Failed" });
            }
        }
    });
});

I don't know why this happen. Could you help me? Thanks in advance.

2 Answers
Related