8000 req.isAuthenticated occasionally fails - race condition? · Issue #306 · jaredhanson/passport · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
req.isAuthenticated occasionally fails - race condition? #306
Closed
@benheymink

Description

@benheymink

I have set up passport to use a custom authentication scheme, but for arguments sake, imagine it authenticates any user to the app. I have a 'landing' page with a login button that issues a post, handled as follows:

app.post('/:id/landing', function (req, res, next) {
  passport.authenticate('myAuth', function (err, user) {
    if (err || !user) {
      return res.redirect('/' + req.params.id + '/landing' + req.body.redirectTo);
    } else {
      req.logIn(user, function () {
        return res.redirect('/' + req.params.id + '/' + req.body.redirectTo);
      });
    }
  })(req, res, next);
});

later, a route handler matches the redirect:

app.all('/:id/', function (req, res) {
  if (req.isAuthenticated()) {
    res.sendFile('index.html', {root: __dirname + '/../frontend/'});
  } else {
    // Instead of redirecting to /landing, simply render it.
    // this gets round safari issue with losing url fragments during a redirect:
    // https://bugs.webkit.org/show_bug.cgi?id=24175
    //
    res.render('landing', {message: '', previousID: '/' + req.params.id});
  }
});

9 times out of ten, this all works fine, but occasionally req.isAuthenticated will return false. If I put some logging in, I can see that before the redirect I have a valid req.user object, but then in the second route handler following the redirect, req.user is undefined. Sometimes it works, sometimes it doesn't! (When it does work, req.user IS defined in the second route handler) Is this issue the same as others have reported around the user not being serialised correctly?

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

      0