I have passport local strategy set up as the following, either of the failure cases work fine. However in the case where I authenticate the server logs "d1" and then hangs, with the request remaining in a "pending" state:
passport.use(
new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
function(email, password, done) {
User.getUser(email, password).then(function(users){
if(users && users.length == 1) {
console.log('d1')
return done(null,users[0]) ;
} else {
console.log('d2')
return done(null, false, { message: 'Incorrect credentials.' });
}
}, function(e){
console.log('d3')
return done(null, false, { message: 'Incorrect credentials.' });
});
})
);
passport.serializeUser(function(user, done) {
console.log('s1')
done(null, user);
});
passport.deserializeUser(function(user, done) {
console.log('s2')
done(null, user);
});
server.post(authPostRoute
, passport.authenticate('local',{
successRedirect : "/ideas",
failureRedirect : "/",
})
);
Ideally I don't want to redirect on success / failure, and would rather return some JSON to the browser. But I can't seem to get this simple scenario to work.
Update - tracing through to passport-local/strategy.js
done above is this function from strategy.js
function verified(err, user, info) {
if (err) { return self.error(err); }
if (!user) { return self.fail(info); }
//console.log(self.success.toString())
self.success(user, info);
}
which is all good. self.success appears to be a valid function, yet it isn't being invoked, as faR as I can tell.
Update II: passport/authenticate.js
looks like success is a function defined in passport/authenticate.js
strategy.success = function(user, info) {
}
I seem to get to
req.logIn(user, options, function(err) {
but never enter the function. Am i missing a definition of logIn?
Update III: logIn
tracing through to passport/http/prequest.js, this line is apparently causing the issue
if (!this._passport) throw new Error('passport.initialize() middleware not in use');
although I thought I enabled it with this:
server.use(passport.initialize());
Edit Update IV: moving passport.initialize
I moved passport initialize earlier in the flow and now the success and error redirect seems to work.
But I am still unable to control the response to the client. I dont want to redirect. Is this a different question?
I experienced similar issue and solved
passport.initialize
to earlier lines solved the problem, thanks a lot.if anyone with this question; for custom logic you can use callbacks like this.