Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passport: different redirect for login and account registration

i'm using the passport module (github authentication) in my app and i want to redirect depending on the action ... i check if it's just a normal login or if the user logs in for the first time.

passport.use(new GitHubStrategy({
    clientID: conf.github.app_id,
    clientSecret: conf.github.app_secret,
    callbackURL: conf.github.callback_url
  },
  function(accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function () {

      // To keep the example simple, the user's GitHub profile is returned to
      // represent the logged-in user.  In a typical application, you would want
      // to associate the GitHub account with a user record in your database,
      // and return that user instead.

      Models_User.findOrCreateUser(profile, function(msg){
        console.log("auth type:" + msg);
      });

      return done(null, profile);

    });
  }
));

in my findOrCreateUser function i check if it's a new user and do all the db action ... for testing i let the function return a msg variable which is only a string that says "login" or "new_registration".

so my question is how to "transport" that variable that i get from findOrCreateUser so that i can redirect accordingly ("/welcome" or "/back_again") after the passport auth is finished.

the other passport code in my app:

// GET /auth/github
//   Use passport.authenticate() as route middleware to authenticate the
//   request.  The first step in GitHub authentication will involve redirecting
//   the user to github.com.  After authorization, GitHubwill redirect the user
//   back to this application at /auth/github/callback
app.get('/auth/github',
  passport.authenticate('github'),
  //passport.authenticate('github', { scope: ['user', 'public_repo', 'gist'] }),
  function(req, res){
    // The request will be redirected to GitHub for authentication, so this
    // function will not be called.
  });

// GET /auth/github/callback
//   Use passport.authenticate() as route middleware to authenticate the
//   request.  If authentication fails, the user will be redirected back to the
//   login page.  Otherwise, the primary route function function will be called,
//   which, in this example, will redirect the user to the home page.
app.get('/auth/github/callback', 
  passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/');
  });
like image 477
toxinlabs Avatar asked Apr 11 '12 18:04

toxinlabs


People also ask

What does req login () do in passport?

Passport exposes a login() function on req (also aliased as logIn() ) that can be used to establish a login session. req.

What is callback URL in passport?

callbackURL is a URL that facebook's web servers themselves will use at the end of the process. Facebook's servers will send a 301 redirect response causing the user's browser to navigate to this URL. So this is essentially a configuration option you are sending to facebook itself, and passport.

What is the use of passport in authentication?

Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.

What is passport local authentication?

The local authentication strategy authenticates users using a username and password. The strategy requires a verify callback, which accepts these credentials and calls done providing a user.


1 Answers

In your verify callback, I would change things up so that the findOrCreateUser function supplies the actual record to the callback, and then pass that through to done(), like so:

Models_User.findOrCreateUser(profile, function(user){
  console.log("auth type:" + msg);
  return done(null, user);
});

// take this out, use the actual model above
//return done(null, profile);

Now, when handling the callback URL after authentication, you can check this user record and see if it was new (I'm assuming it has an isNew property here):

app.get('/auth/github/callback', 
  passport.authenticate('github', { failureRedirect: '/login' }),
  function(req, res) {
    // successful auth, user is set at req.user.  redirect as necessary.
    if (req.user.isNew) { return res.redirect('/back_again'); }
    res.redirect('/welcome');
  });
like image 73
Jared Hanson Avatar answered Oct 07 '22 12:10

Jared Hanson