Search code examples
node.jsinstagraminstagram-api

How to check if user is already logged into instagram


I am currently using node.js, express, and mongodb for an instagram app. I have found that many times I would like to know whether or not a user is already logged into instagram (be it through my app via the instagram-node authentication or through instagram's actual website).

Is there an easy way to do this?


Solution

  • I ended up using passport to solve this problem. Passport conveniently takes care of handling instagram authorization and they even include an example app to see how it all works. https://github.com/jaredhanson/passport-instagram/blob/master/examples/login/app.js

    function ensureAuthenticated(req, res, next) {
      if (req.isAuthenticated()) { return next(); }
      res.redirect('/login')
    }
    

    Is especially useful since it can be placed at the top of your routing file and all the routes underneath it will first check to see if the user is authenticated.