Passport (local) is hanging on Authentication

2019-09-06 02:42发布

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?

1条回答
ら.Afraid
2楼-- · 2019-09-06 03:20

I experienced similar issue and solved passport.initialize to earlier lines solved the problem, thanks a lot.

But I am still unable to control the response to the client. I dont want to redirect. Is this a different question?

if anyone with this question; for custom logic you can use callbacks like this.

router.post('/login', function(req, res, next) {
    passport.authenticate('local-signin', function(err, user, info) {
      if (err) { return next(err); }
      if (!user) {
        return res.render('account/login', {
          pageTitle: 'Sign in',
          form: req.body
        });
      }
      req.logIn(user, function(err) {
        if (err) { return next(err); }
        return res.redirect('/myaccount');
      });
    })(req, res, next);
});
查看更多
登录 后发表回答