Passport is an authentication middleware that is extremely flexible and modular. Any application might need to incorporate a user's information through third-party services. In this particular case, the application will send out a "connect" request with the user's Twitter or Facebook accounts.

Authorization is handled by calling passport.authorize(). If the authorization is granted, the result by the verify callback shall be assigned to the req.account.

Here's how authorization of a Twitter account is handled in Passport.

Example

                                        

app.get('/connect/twitter',
  passport.authorize('twitter-authz', { failureRedirect: '/account' })
);

app.get('/connect/twitter/callback',
  passport.authorize('twitter-authz', { failureRedirect: '/account' }),
  function(req, res) {
    var user = req.user;
    var account = req.account;

    account.userId = user.id;
    account.save(function(err) {
      if (err) { return self.error(err); }
      self.redirect('/');
    });
  }
);

Suggest An Answer

No suggestions Available!