Passport-local-mongoose : Authenticate user right after registration Passport-local-mongoose : Authenticate user right after registration mongoose mongoose

Passport-local-mongoose : Authenticate user right after registration


According to the API documentation of passport-local-mongoose at https://github.com/saintedlama/passport-local-mongooseyou register a user by calling register(user, password, cb)

So your register.js should look something like this

 ... if (errors) {     res.render("register" , { errors : errors } ); } else {     var newUser = new User({         username : username,         email : email,         tel : tel,         country : country     });     User.register(newUser, password, function(err, user) {        if (errors) {            // handle the error        }        passport.authenticate("local")(req, res, function() {            // redirect user or do whatever you want        });     }); }

and don't forget to require passport in register.js


it seems your passport config is not setup correctly.

You have to tell passport how to handle success and failure in order to get it to log you in.

From the documentation . (Create a config.js file you will import in your main file);

var passport = require('passport')  , LocalStrategy = require('passport-local').Strategy,  , User = require('../models/user');passport.use(new LocalStrategy(  function(username, password, done) {    User.findOne({ username: username }, function(err, user) {      if (err) { return done(err); }      if (!user) {        return done(null, false, { message: 'Incorrect username.' });      }      if (!user.validPassword(password)) {        return done(null, false, { message: 'Incorrect password.' });      }      return done(null, user);    });  }));