surfearth
surfearth

Reputation: 3147

Passport Authentication immediately after New User Registration

I'm trying to authenticate and login a user immediately after submitting a POST on the /register form. Ideally, I would like users to be able to register and then be redirected immediately to the dashboard without having to enter their credentials again.

My server is using Passport 0.1.17 with the local strategy configured to use email address and password for login. The current code is:

app.post('/register', function(req, res) {

  // attach POST to new User variable
  var registerUser = new User({ email: req.body.email, password: req.body.password, name: req.body.name });

  // save registerUser Mongo
  registerUser.save(function(err) {
    if(err) {
      console.log(err);
    } else {
      console.log('registerUser: ' + registerUser.email + " saved.");
    }
  });

  // here is where I am trying to authenticate and then redirect
  passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
  res.redirect('/dashboard');
  });

How would I refactor this code to save the new user, then authenticate and finally redirect to the dashboard?

Thanks in advance!

Upvotes: 24

Views: 13535

Answers (2)

daCoda
daCoda

Reputation: 3855

I think you're looking for the register method, which will register (and hide the password)

https://www.npmjs.com/package/passport-local-mongoose (search the register method).

app.post('/register', function(req, res) {

  // New user variable created.
  // Note: Password is not part of the new User variable; you don't want to simply store sensitive information in the database.
  var registerUser = new User({ email: req.body.email, name: req.body.name });

  // Register new user. Note the 2nd variable (password). If registration's successful (no errors), redirect.
  registerUser.register(registerUser, req.body.password, function (err, newUser){
      if(!err){
           passport.authenticate('local', req, res, function(){
                   res.redirect('/dashboard');                     
           });
       }
  });
});

Upvotes: 0

surfearth
surfearth

Reputation: 3147

Here's the solution I came up with after reading about req.login:

app.post('/register', function(req, res) {
  // attach POST to user schema
  var user = new User({ email: req.body.email, password: req.body.password, name: req.body.name });
  // save in Mongo
  user.save(function(err) {
    if(err) {
      console.log(err);
    } else {
      console.log('user: ' + user.email + " saved.");
      req.login(user, function(err) {
        if (err) {
          console.log(err);
        }
        return res.redirect('/dashboard');
      });
    }
  });
});

I would like to clean it up a bit and think that the err section could be more robust, but this is a functioning solution. Note that is someone else implements this, they should be aware that it is tailored to using the passport-local strategy with email instead of username.

Upvotes: 30

Related Questions