Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

req.user not set when using a custom callback for authenticate #255

Open
PymZoR opened this issue Jun 29, 2014 · 3 comments
Open

req.user not set when using a custom callback for authenticate #255

PymZoR opened this issue Jun 29, 2014 · 3 comments

Comments

@PymZoR
Copy link

PymZoR commented Jun 29, 2014

Hi,
I've been struggling to user req.user using a custom callback for authenticate.
The code i'm using is the following:

router.post('/in', function(req, res, next) {
    passport.authenticate('local', function(err, user, info) {
        if (err) {
            return next(err); // Error 500
        }

        if (!user) {
            //Authentication failed
            return res.json(401, { "error": info.message }); 
        }
        //Authentication successful
        res.send(200);
    })(req, res, next);
}); `

With this code, accessing req.user return undifined.
Sadly, adding this line
req.user = user
after
//Authentication successful
won't work.
However, adding
req.session.user = user works !

In addition, without the custom callback, req.user is correctly set.
I think I've pointed out an unexpected behavior, or maybe I'm doing something wrong.

Regards

@BohdanTkachenko
Copy link

Got the same problem. It appeared after upgrading from Express 3 to Express 4

@WaldoJeffers
Copy link

I think it is related to the fact that you are using a custom callback without using the req.login method, not to whether you're using Express 3 or 4.

From the docs, "Note that when using a custom callback, it becomes the application's responsibility to establish a session (by calling req.login()) and send a response."

You should add this code if your authentication is successful :

 req.logIn(user, function(err) {
      if (err) { return next(err); }
      return res.redirect('/users/' + user.username);
    });

@tanis2000
Copy link

@WaldoJeffers I stumbled upon this same problem and your answer is correct. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants