Search code examples
node.jsexpressinstagraminstagram-api

res.redirect doesn't redirect Node/Express.js


I'm using Express for my webapp and I'm checking to see if some value doesn't exists in session, redirect the user to instagram login page. But in the console I keep getting 302 code and the browser doesn't redirect the user here is my code. I'm sending my requests using Ajax.

    function requireLogin (req, res, next) {
     if (!req.token) 
        res.redirect('/auth');
     else 
        next();      
    };

 app.get('/auth',function(req,res,next){
   res.redirect("https://api.instagram.com/oauth/authorize/?client_id="+client_id+"&redirect_uri="+redirect_uri+"&score=public_content+likes+comments+follower_list+relationships&response_type=code");
});

I checked and app.get('/auth') gets called but it seems the second redirect to instagram doesn't do anything.


Solution

  • The problem was that res.redirect does not redirect the user if you are using Ajax. So I passed the url to client side and used window.location.replace(url);