Node js Next 不是护照策略错误中的函数

Node js Next is not a function in passport strategy error

登录系统似乎无法正常工作。我正在使用包含用户信息的 json 文件,并且正在使用 node.js、快递和护照。这是我的代码(删节后只包含相关信息)。

Index.js

const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
const myDB = require('./jDB');
const passport = require('passport')
  , LocalStrategy = require('passport-local').Strategy;
const app = express();
const bodyParser = require('body-parser');

app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(passport.initialize());
app.use(passport.session());

app.get('/success', (req, res) => res.send("Welcome "+req.query.username+"!!"));
app.get('/error', (req, res) => res.send("error logging in"));

passport.serializeUser(function(user, cb) {
  cb(null, user.id);
});

passport.deserializeUser(function(id, cb) {
  User.findById(id, function(err, user) {
    cb(err, user);
  });
});

passport.use(new LocalStrategy({

  usernameField: 'username',
  passwordfield : 'password'

},
  function(username, password, done) {
  console.log('I was given ' + username + " and " + password);
  if(myDB.login(username, password)) {
      console.log('Logged in');
      return done(null, user); 
  } else {
      console.log('Failed login');
      return done(null, false);
  }
}));


function myAuth(req, res) {
  passport.authenticate('local', { 
    successRedirect: '/success', 
    failureRedirect: '/error', 
  })(req, res);


}

app.get('/login', (req, res) => res.render('pages/login', { root : __dirname}));

app.post('/login', (req, res) => myAuth(req, res));

app.listen(PORT, () => console.log(`Listening on ${ PORT }`));  

jDB.js

const login = require('./json/users.json');

module.exports = {
    login : function(mUser, mPass) {
        var cryptPass = crypto.createHmac('sha256', mPass).digest('hex');
        for(var exUser in login) {
            console.log(json.stringify(login[exUser]) + " against " + mUser);
            if(login[exUser].username == mUser && login[exUser].password == cryptPass) {
                return true;
            }
        } 
        return false;

    }

以及 login.ejs 的表格部分:

<form action='/login' method='post'>
            <div class="form-group">
              <label for="username">Username</label>
              <input type="text" class="form-control" name="username">
            </div>
            <div class="form-group">
              <label for="pasword">Password</label>
              <input type="password" class="form-control" name="password">
            </div>
            <button type="submit" class="btn btn-primary">Login</button>
          </form>

每当我尝试使用正确的用户名和密码详细信息登录时,我都会收到以下错误:

TypeError: next is not a function
    at Strategy.strategy.error (/home/tommy/carrers-week/node_modules/passport/lib/middleware/authenticate.js:356:9)
    at Strategy.authenticate (/home/tommy/carrers-week/node_modules/passport-local/lib/strategy.js:93:17)
    at attempt (/home/tommy/carrers-week/node_modules/passport/lib/middleware/authenticate.js:361:16)
    at authenticate (/home/tommy/carrers-week/node_modules/passport/lib/middleware/authenticate.js:362:7)
    at myAuth (/home/tommy/carrers-week/index.js:195:5)
    at app.post (/home/tommy/carrers-week/index.js:202:34)
    at Layer.handle [as handle_request] (/home/tommy/carrers-week/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/tommy/carrers-week/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/tommy/carrers-week/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/tommy/carrers-week/node_modules/express/lib/router/layer.js:95:5)

我真的不知道如何解决这个问题,所以在此先感谢您的帮助!

从错误跟踪可以看出此应用在

内抛出
passport.authenticate

此外,它还为next方法感到悲伤。 我会做的一件事是在

范围内确定此调用的范围
function next() {
  console.log(arguments)
}

或直接提供:

function myAuth(req, res, next) {
  passport.authenticate('local', { 
    successRedirect: '/success', 
    failureRedirect: '/error', 
  })(req, res, next);


}

app.post('/login', (req, res, next) => myAuth(req, res, next));

要获得更多见解,可以查看

passport-local/blob/master/lib/strategy.js:93

try {
    if (self._passReqToCallback) {
      this._verify(req, username, password, verified);
    } else {
      this._verify(username, password, verified);
    }
  } catch (ex) {
    return self.error(ex); //93
  }

passport/blob/master/lib/middleware/authenticate.js:356

  strategy.error = function(err) {
    if (callback) {
      return callback(err);
    }

    next(err); //356
  };

等瞧:

没有提供给本地策略的回调会触发next()调用