Reputation: 639
I have tried to setup a simple local strategy authentification with express4 / passportjs, but the authenticate function is never called : here's my code
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
app.use(compress());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
/* AUTHENTIFICATION */
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
passport.use(new LocalStrategy(
function(username, password, done) { //THIS FUNCTION IS NEVER CALLED
console.log("Local strategy");
}
));
app.post('/login', function(req, res) {
console.log("user : ", req.body.username);
console.log("password : ", req.body.password);
passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/login' })
});
/* !AUTHENTIFICATION */
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'bower_components')));
app.use(express.static(path.join(__dirname, 'controllers')));
app.use(express.static(path.join(__dirname, 'modules')));
app.use('/', routes);
app.use('/users', users);
When i send a post request in /login, i see in the logs the username and password but the password.authentificate
method doesnt do anything.
It should call the LocalStrategy
function and console.log('strategy') but it doesn't.
Does someone know how to fix it ?
Thanks
Upvotes: 1
Views: 356
Reputation: 4006
authenticate()'s function signature is standard Connect middleware
from here.
So reference of req, res, next
should be passed
So you can use it either as route middleware
app.post('/login', passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/login' }));
Or
app.post('/login', function(req, res, next) {
passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/login' })(req, res, next);
});
Upvotes: 2