What does your index.html or login page look like? At your post, you need to make sure that you send at least something in the body with the username and password fields. If you send a message without them, you will receive a Missing credentials error message. If you want to change them, you can change the settings as shown in this manual .
You can verify this yourself by adding a route to fix the login error, and specify this route when calling passport.authenticate .
app.post('/login', passport.authenticate('local', { failureRedirect: '/loginerror', failureFlash: true }), function(req, res) { res.redirect('/'); }); app.get('/loginerror') function(req,res) { console.log(req.flash('error')); res.redirect('/login'); }
I modified your example to add the necessary forms. In addition, if there is any error, it is displayed on the login page. For example, if you simply enter a username rather than a password, you will see the "Missing credentials" error message. Hope this helps!
var http = require('http'), express = require('express'), passport = require('passport'), LocalStrategy = require('passport-local').Strategy, flash = require('connect-flash'); var port = process.env.PORT || 8080; passport.serializeUser(function(user, done) { done(null, user); }); passport.deserializeUser(function(obj, done) { done(null, obj); }); passport.use(new LocalStrategy( function(username, password, done) { console.log("LocalStrategy working..."); return done(null, { id: 1, username: 'Joe', password: 'schmo'}); } )); var app = express(); app.configure(function(){ app.use(express.static(__dirname + '/app')); app.use(express.cookieParser('big secret')); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieSession()); app.use(flash()); app.use(passport.initialize()); app.use(passport.session()); app.use(app.router); }); app.get('/', function(req, res){ var username = "not logged in"; if (req.user) { username = req.user.username; } var body = '<html><body>'; body = body + '<p>' + username + '</p>'; body = body + '<a href="/login">login</a>' body = body + '</body></html>' res.send(body); }); app.get('/login', function(req, res){ var message = req.flash('error'); var body = '<div><p>' + message + '</p></div>'; body = body + '<form action="/login" method="post">'; body = body + '<div><label>Username:</label>'; body = body + '<input type="text" name="username"/><br/></div>'; body = body + '<div><label>Password:</label>'; body = body + '<input type="password" name="password"/></div>'; body = body + '<div><input type="submit" value="Submit"/></div></form>'; res.send(body); }); app.post('/login', passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }), function(req, res) { res.redirect('/'); }); app.listen(port);