护照js缺少凭据 [英] passport js missing credentials

查看:63
本文介绍了护照js缺少凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Been working on this for a few hours now, pretty frustrating...

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

When I run this, i used console.log, output was { message: 'Missing credentials' }, which leads me to believe that the body parser is not properly parsing the body message. When I use this route however...

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'},这表示 {password: 'password', email: 'email@email.com'}code>req.body 变量设置正确且可用.

When I used console.log, the output was {password: 'password', email: 'email@email.com'}, which indicates that the req.body variables are being set properly and are available.

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

推荐答案

I see your req.body contains {password: 'password', email: 'email@email.com'}.email 不是passportjs 想要的,但是username 是.您可以更改 HTML/JS 上的输入名称,也可以更改 Passportjs 在 req.body 中寻找的默认参数.您需要在定义策略的地方应用这些更改.

I see your req.body contains {password: 'password', email: 'email@email.com'}. email is not what passportjs is looking for, but username is. You can either change your input names on your HTML/JS, or you can change the default parameters passportjs is looking for in req.body. You need to apply these changes where you define your strategy.

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

这篇关于护照js缺少凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆