护照JS缺少凭据。

26

我已经花了几个小时在这上面了,感觉很沮丧...

router.post('/', passport.authenticate('local-signup', function(err, user, info) {
  console.log(err);
}), function(req, res){
  console.log(req);
  res.setHeader('Content-Type', 'application/json');
  res.send(JSON.stringify({ a: 1 }));
});
当我运行这个程序时,我使用了console.log,输出结果为{ message: 'Missing credentials' },这使我相信body parser没有正确解析消息主体。然而,当我使用这个路由时...
router.post('/', function(req, res){
  console.log(req.body);
  res.setHeader('Content-Type', 'application/json');
  res.send(JSON.stringify({ a: 1 }));
});

我使用console.log时,输出为{password: 'password', email: 'email@email.com'},这表明req.body变量被正确设置并可用。

app.js

var express = require('express');
var app = express();
var routes = require("./config/routes.config");
var models = require("./config/models.config");
var session = require('express-session');
var bodyParser = require('body-parser');

models.forEach(function(model){
  GLOBAL[model] = require('./models/'+model+".model");
});
var passport = require("./config/passport.config");

app.use( bodyParser.urlencoded({ extended: true }) );
app.use(session({ secret: 'simpleExpressMVC', resave: true, saveUninitialized: true  }));
app.use(passport.initialize());
app.use(passport.session());

我也遇到了同样的问题,进行了很多搜索,但是没有找到任何解决方案 :-( - Vikash Kumar
9个回答

62

我看到你的req.body包含{password: 'password', email: 'email@email.com'}passportjs不是在寻找email,而是在寻找username。你可以在你的HTML/JS中更改输入名称,或者你可以更改req.body中passportjs正在寻找的默认参数。您需要在定义策略的位置应用这些更改。

passport.use(new LocalStrategy({ // or whatever you want to use
    usernameField: 'email',    // define the parameter in req.body that passport can use as username and password
    passwordField: 'password'
  },
  function(username, password, done) { // depending on your strategy, you might not need this function ...
    // ...
  }
));

8
在 Router.post 中:
router.post('/', passport.authenticate('local-signup', {
    successRedirect: '/dashboad',
    failureRedirect: '/',
    badRequestMessage: 'Your message you want to change.', //missing credentials
    failureFlash: true
}, function(req, res, next) {
...

7
我知道这里有很多答案,但这是最清晰的。
从官方护照文件中 Passportjs documentation “默认情况下,LocalStrategy 希望在名为用户名和密码的参数中找到凭据。如果您的网站更喜欢以不同的方式命名这些字段,则可以使用选项更改默认值。”
因此,如果您发现自己有此问题,您有两个解决方案(很可能)。
  1. Rename your body parameters in your front-end as username and password

  2. Define to your passport instance how you're going to name them with the following code:

    passport.use(new LocalStrategy({
        usernameField: 'email', //can be 'email' or 'whateveryouwant'
        passwordField: 'passwd' //same thing here
      },
      function(username, password, done) {
        // ...
      }
    ));
    

1
谢谢,你的回答进一步澄清并扩展了已接受的答案,帮助我理解了相同的错误。非常感谢。 - xv8
很高兴能帮助到有人。干杯 - Lheonair

1
可能是字段类型出错了,因此passport.js无法识别正确的字段并出现错误。
更正:请检查.ejs文件中您的用户名和密码字段是否正确声明了类型。 type="password" id="password" name="password"//正确指定。
您可能已经在ejs文件中输入了Type:"text",并告诉passport寻找type:"password" 提示:检查类型字段中的拼写错误。 例如:type:password在ejs中&type:Password

2
你好,欢迎来到SO!请阅读tour如何撰写优质答案?。例如,在开头写“愚蠢”的话是没有意义的,这可能会被认为是冒犯的。尽量在回答中保持主题相关性。 - Tomer Shetah
明白了!感谢你的帮助。 - Anshul Tyagi

1
在我的情况下,导致这种情况的原因是我使用的body-parser或express配置不正确,导致我的应用程序无法接收来自表单的输入。所以我做了以下操作:
// configuring express
app.use(express.static(path.join(__dirname, "public")));
// body-parser 
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

0
在我的情况下,我只是将usernameField拼错成了usernameFiled
请确保正确拼写usernameFieldpasswordField

0
我的问题是拼写错误。 我写成了usernameFeild: 'email', 然而,正确的应该是usernameField: 'email',

1
要是那么简单就好了。 - gfdb

0
如果您正在使用Passport Local Mongoose作为用户模型的插件,请尝试以下操作:
passport.use(new LocalStrategy({
  usernameField: 'email'
}, User.authenticate()));

0
在我的情况下,将所有字段添加到配置文件中有效:
passport.use(new LocalStrategy({
  usernameField : 'email',
  passwordField : 'password',
  passReqToCallback : true
}))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接