More Passport.js woes - hangs on form submission

点点圈 提交于 2019-12-10 00:54:40

问题


I am setting up what I thought would be the simplest auth possibly - a site loads on a log in screen, user enters credentials in a form, on submission I am using Passport.JS and Sequelize to check the credentials. Most all of this is copied from various tutorials, or the Passport website itself. No matter what I do, change or attempt though, the site just hangs as soon as I click the form submit button. In the dev tools network tab I just shows the post request to /login as pending.

To eliminate all possible added problems, I stripped out Sequelize, and used hard coded users, ala the local use example on the passport github page. Still, no change, to Sequelize was not the problem.

I'm sure it's something dumb. I've tried the obvious solutions, like making sure my form was sending 'username' and 'password'.

My form looks like this:

form(method="post", action="/login")
  fieldset
    legend Please Log In
    label(for="username") Username:
    input#username(type='text', name="username", autocomplete="off")
    label(for="password") Password:
    input#password(type='password', name="password")
    button#login(type="submit") Log In

In node my app.js uses a router, which includes this:

var login = require('../app/controllers/login');
app.get('/', login.index);
app.post('/login', login.auth);

The page load ok on the get request. On post it directs to this:

exports.auth = function (req, res, next) {
  console.log(req.body);
   passport.authenticate('local', {
    successRedirect: '/home',
    failureRedirect: '/',
    failureFlash: true
  });
};

The console.log(req.body) comes up fine, showing the form values, so we are ok to that point. But nothing after. This is the last part:

passport.use(new LocalStrategy(
  function(username, password, done) {
    console.log('local strat invoked');
    findByUsername(username, function(err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
      if (user.password != password) { return done(null, false, { message: 'Invalid password' }); }
      return done(null, user);
    });
  }
));

I swapped out the code that used Sequelize to check the DB for the user with this findByUsername function (copied and pasted straight from the above mentioned post on the passport github page), but as the console.log('local strat invoked') is never coming up, I'm guessing nothing after that point even matters.

Also includes are the passport.serializeUser and passport.deserializeUser, but I cant imagine those are the problem at the stage where I am stuck.

Any idea what step I am missing to get this working?


回答1:


Passport is middleware, if you embed it in a route handler function, you need to invoke it:

exports.auth = function (req, res, next) {
  console.log(req.body);
  passport.authenticate('local', {
    successRedirect: '/home',
    failureRedirect: '/',
    failureFlash: true
  })(req, res, next);  // <-- NEED TO INVOKE WITH req, res, next
};

or, more simply use it as middleware (which is the recommended approach:

exports.auth = passport.authenticate('local', {
  successRedirect: '/home',
  failureRedirect: '/',
  failureFlash: true
});


来源:https://stackoverflow.com/questions/20626183/more-passport-js-woes-hangs-on-form-submission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!