passport local strategy not getting called

前端 未结 14 801
夕颜
夕颜 2020-12-13 04:31

I\'m sure I\'m missing something really obvious here, but I can\'t figure this out. The function I\'ve passed to the LocalStrategy constructor doesn\'t get called when the l

14条回答
  •  悲哀的现实
    2020-12-13 04:50

    I recently came across this problem and it can be caused by a number of things. First thing to check is ensuring that the bodyParser is set up for express, which I see that you have done.

    app.use(express.bodyParser());
    

    The next thing is ensuring that the form you are submitting to the route contains both a username AND password, and the user must also enter something in both fields. I was stumped for a bit testing it and not actually putting anything in the password field while testing :) Passport requires BOTH to execute the LocalStrategy verification function passed to passport.authenticate('local').

    Your example also seems to be set up to capture both username and password properly, but in any case, you should try testing that the body is being parsed properly even without passport:

    app.post('/auth', function(req, res){
      console.log("body parsing", req.body);
      //should be something like: {username: YOURUSERNAME, password: YOURPASSWORD}
    });
    

    Else

    Did you try adding a request handler to your /auth route?

    app.post('/auth', passport.authenticate('local'), function(req, res){
      console.log("passport user", req.user);
    });
    

    or

    app.post('/auth', passport.authenticate('local', { successRedirect: '/', failureRedirect: '/auth' }));
    

提交回复
热议问题