title: How to get the Instagram username and accesstoken in NodeJS
body: <p>I am using Instagram-passport for login to my app. How can I get the username and access token after the user logs in? I could not access the 'profile' in the middleware section.</p>
  
  <pre><code>//serialize user in the session
  passport.serializeUser(function (user, done) {
      done(null, user);
  });
  //deserialize user in the session
  passport.deserializeUser(function (id, done) {
      User.findById(id, function (err, user) {
          done(err, user);
      });
  });
  //callback after successfull login of instagram
  exports.callback = function (req, res) {
      // Successful authentication, redirect home.
      console.log('calling');
      var code = req.query.code;
      //console.log('accessToken:' + accessToken);
      console.log(req.session)
      getOption(code, function (option) {
          //console.log('option from getOption callback ' + util.inspect(option, false, null));
          request(option, function (error, response, body) {
              console.log('response inside request: ' + util.inspect(body, false, null));
              fs.writeFile("test", JSON.stringify(error), function (err) {
                  if (err) {
                      console.log(err);
                  } else {
                      console.log("The file was saved!");
                  }
              });
              res.writeHead(200, {
                  'Content-Type': 'text/json'
              });
              //res.end(response);
  
  
          });
      });
      // Successful authentication, redirect home
  }
  //function to get the option for the first parameter in the request function
  function getOption(code, callback) {
      console.log('code from getOption ' + code)
      var options = {
          url: 'https://api.instagram.com/oauth/access_token',
          headers: {
              'client_id': 'client-id',
              'client_secret': 'client-secret`enter code here`',
              'grant_type': 'authorization_code',
              'redirect_uri': '/instagramcallback',
              'code': code
          }
      };
      callback(options);
  }
  //middleware 
  passport.use(new InstagramStrategy({
      clientID: 'my client-id',
      clientSecret: 'my client secret',
      callbackURL: "/instagramcallback"
  },
  function (accessToken, refreshToken, profile, done) {
      // asynchronous verification, for effect...
           global.accessToken=accessToken;
          // To keep the example simple, the user's Instagram profile is returned to
          // represent the logged-in user. In a typical application, you would want
          // to associate the Instagram account with a user record in your database,
          // and return that user instead.
          console.log('client profile:'+profile);
          return done(null, profile);
  }
  ));
  </code></pre>