title: Passport-local-mongoose : Authenticate user right after registration
body: <p>I am trying to use <a href="https://www.npmjs.com/package/passport-local-mongoose" rel="nofollow">passport-local-mongoose</a> to authenticate a user after submitting a POST on the /register form.
  I can't figure out why i always get "unauthorized" &amp; oddly no data is submitted on the database (it works if i remove "passport.authenticate('local') from app.post("/register") ) <br/></p>
  
  <p>any ideas ?</p>
  
  <p><br/>
  <strong>app.js :</strong></p>
  
  <pre><code>    // ====== Dependencies
  var express = require('express');
      bodyParser = require('body-parser');
      validator = require('express-validator');
      cookieParser = require('cookie-parser');
      expressSession = require('express-session');
      passport = require('passport');
      LocalStrategy = require('passport-local').Strategy;
      auth = require('./controllers/auth');
      registration = require('./controllers/register');
      User = require('./models/user');
  // ====== Options
  var app = express()
  .use(bodyParser.urlencoded({ extended: false }))
  .use(bodyParser.json())
  .use(validator())
  .set('view engine','ejs')
  .use(express.static("views"))
  .use(cookieParser())
  .use(expressSession({
      secret : 'soooosecret',
      resave : true,
      saveUninitialized : false
  }))
  .use(passport.initialize())
  .use(passport.session());
  passport.use(new LocalStrategy(User.authenticate()));
  passport.serializeUser(User.serializeUser());
  passport.deserializeUser(User.deserializeUser());
  
      app.post('/register', passport.authenticate('local'), registration.reg );
  
  app.listen(8080);
  console.log('Server running at 8080');
  </code></pre>
  
  <p><strong>register.js :</strong></p>
  
  <pre><code>var User = require('../models/user');
  
  var reg = function (req, res) {
  
      /* some form validation... */
      var errors = req.validationErrors();
  
       if (errors) {
           res.render("register" , { errors : errors } );
       }
       else {
           var newUser = new User({
               username : username,
               password : password,
               email : email,
               tel : tel,
               country : country
           });
  
           User.createUser(newUser);
       }
  
  }
  module.exports = { reg : reg }
  </code></pre>
  
  <p><strong>user.js :</strong> </p>
  
  <pre><code>var mongoose = require('mongoose');
      mongoose.connect('mongodb://localhost/mvpem');
  
  var passportLocalMongoose = require('passport-local-mongoose');
  
   var db = mongoose.connection;
  
  var Schema = mongoose.Schema;
  
  var UserSchema = new Schema({
      username    : String,
      password     : String,
      email      : String,
      tel      : Number,
      country : String
  });
  
  UserSchema.plugin(passportLocalMongoose);
  
  module.exports = mongoose.model('User', UserSchema);
  
  module.exports.createUser = function(newUser) {
      newUser.save(); 
  }
  </code></pre>