【问题标题】:passport js missing credentials护照js缺少凭据
【发布时间】:2021-03-28 06:48:22
【问题描述】:

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

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

【问题讨论】:

  • 我也面临同样的问题,做了很多搜索,但找不到任何解决方案:-(

标签: passport.js passport-local


【解决方案1】:

我看到您的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 ...
    // ...
  }
));

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      来自官方护照文档

      Passportjs documentation

      "默认情况下,LocalStrategy 期望在参数中找到凭据 命名的用户名和密码。如果您的网站更喜欢命名这些字段 不同的是,可以使用选项来更改默认值。”

      因此,如果您发现自己遇到此问题,您有两个解决方案(很可能)。

      1. 在你的前端重命名你的body参数为usernamepassword

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

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

      【讨论】:

      • 谢谢,您的回答得到了进一步澄清并扩展了已接受的答案,这有助于我解决我遇到的同样错误。非常感谢。
      • 很高兴它帮助了某人。干杯
      【解决方案4】:

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

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

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

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

      【讨论】:

      • 您好,欢迎来到 SO!请阅读tourHow do I write a good answer? 例如,在乞讨中写傻是没有意义的。它可能被认为是冒犯性的。尽量在你的答案中保持主题。
      • 明白!感谢您的帮助。
      【解决方案5】:

      在我自己的情况下,这是因为我的 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 }));
      

      【讨论】:

        【解决方案6】:

        就我而言,我只是想念将usernameField 拼写为usernameFiled

        确保usernameFieldpasswordField 拼写正确。

        【讨论】:

          【解决方案7】:

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

          不过应该​​是usernameField: 'email',

          【讨论】:

          • 要是这么简单就好了
          【解决方案8】:

          如果您使用护照本地猫鼬作为您的用户模型的插件。

          试试这个:

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

          【讨论】:

            【解决方案9】:

            在我的例子中,添加 config works 中的所有字段:

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

            【讨论】:

              猜你喜欢
              • 2018-06-02
              • 2015-08-22
              • 2020-05-12
              • 2019-03-02
              • 1970-01-01
              • 2020-12-19
              • 2020-09-26
              • 2013-03-06
              • 2017-06-19
              相关资源
              最近更新 更多