Im having the problem that when the login form is submitted, the application hangs and doesn't get redirected.
I have tried debugging the issue but I can't figure out what's going on.
So far what I have found is (on login submit) the following are called:
Login POST:
router.post('/login', passport.authenticate('local-login', {
successRedirect : '/', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}) );
Middleware gets called, but the redirects never occur.
Authentication using (passport-local) LocalStrategy:
passport.use(
'local-login',
new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) { // callback with email and password from our form
connection.query("SELECT * FROM users WHERE email=?",[email], function(err, rows){
if (err)
return done(err);
if (!rows.length) {
return done(null, false, req.flash('login', 'Oops! Wrong email or password')); // req.flash is the way to set flashdata using connect-flash
}
// if the user is found but the password is wrong
if (sha1(password) != rows[0].password) {
return done(null, false, req.flash('login', 'Oops! Wrong email or password'));
}
// all is well, return successful user
return done(null, rows[0]);
});
})
);
Debug shows that this executes successfully.
SerializeUser:
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.serializeUser receives user object as expected.
DeserializeUser:
passport.deserializeUser(function(id, done) {
connection.query("SELECT * FROM users WHERE id=? ",[id], function(err, rows){
done(err, rows[0]);
});
});
Here I'm not sure what happens. The application hangs. While debugging, I can't get past the following line:
break in net.js:587
585 // procedure. No need to wait for all the data to be consumed.
586 self.emit('_socketEnd');
>587 }
588
589
debug> n
debug> n
I keep pressing 'n' or 'c' but nothing happens.
Can I get some advice on what I'm missing?
Also, can someone help me better understand what the done function does in these authentication methods?
UPDATE:
Function to check that user is logged in:
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
}