Skip to content

req.login redirect is done before the user info is saved in the session #401

@franckl

Description

@franckl

I have a simple login page with local auth and custom callback.
When the login is successful, I redirect the user to the main page and check if (req.isAuthenticated()) is true.
50% of the time, req.isAuthenticated() returns false. It seems that the redirect is happening before the session information is updated. It didn't notice this issue when I was using the standard local auth options without custom callback.

Any idea ?

    self.app.post('/login', function (req, res, next) {
        passport.authenticate('local', function (err, user, info) {
            if (err) {
                return next(err);
            }

            var sanitizedEmail = validator.escape(req.body.username);
            if (!user) {
                req.flash('error', 'Wrong email or password');
                req.flash('emailL', sanitizedEmail);
                return res.redirect('/login');
            }
                req.logIn(user, function (err) {
                    if (err) {
                        return next(err);
                    }
                        return res.redirect('/main/');
                });
        })(req, res, next);
    });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions