护照 js 缺少凭据

passport js missing credentials

现在已经为此工作了几个小时,非常令人沮丧...

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' },这让我相信正文解析器没有正确解析正文消息。但是,当我使用这条路线时...

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());

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

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 ...
    // ...
  }
));

在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) {
...

在我的例子中,我只是将 usernameField 拼错为 usernameFiled

确保拼写正确 usernameFieldpasswordField

我的是拼写错误问题。 我有 usernameFeild: 'email',

不过应该是usernameField: 'email',

如果您使用 passport local mongoose 作为用户模型的插件。

试试这个:

passport.use(new LocalStrategy({
  usernameField: 'email'
}, User.authenticate()));

在我的例子中,添加配置工程中的所有字段:

passport.use(new LocalStrategy({
  usernameField : 'email',
  passwordField : 'password',
  passReqToCallback : true
}))

字段类型可能有误,这就是为什么 passport.js 无法识别正确的字段并给出错误。

更正:如果您的用户名和密码字段正确说明类型,请检查 .ejs 文件。 type="password" id="password" name="password"//正确指定。

你可能做的是:在 ejs 文件中 Type:"text" 并告诉 passport 寻找 type:"password"

提示:检查类型字段中的拼写错误。 例如:ejs 中的 type:password & type:Password

在我自己的案例中,原因是我的正文解析器或快速配置,因此我的应用程序没有收到来自表单的输入。所以我这样做了:

// configuring express
app.use(express.static(path.join(__dirname, "public")));
// body-parser 
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

我知道这里有很多答案,但这已经很清楚了。

来自官方护照文件

Passportjs documentation

"By default, LocalStrategy expects to find credentials in parameters named username and password. If your site prefers to name these fields differently, options are available to change the defaults."

因此,如果您遇到这个问题,您有两种解决方案(很可能)。

  1. 在前端将您的正文参数重命名为 usernamepassword

  2. 使用以下代码为您的护照实例定义您将如何命名它们:

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