节点JS Passport - 400错误的注册请求

时间:2017-09-21 20:29:36

标签: node.js postgresql passport.js

我尝试在我的网络应用程序上实现一个注册表,使用Node JS和Passport.js进行本地身份验证,但是当我在Insomnia上发布请求时(例如Postman),我有这个错误:

ctx := MakeMyContext()
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/myroute", nil)
// headers etc
MyHandler(ctx,w,r)
// Validate status, body, headers, whatever in r

我不明白,因为我已经按照我在Google上找到的教程来执行此操作。 这是路线(auth.js):

Bad Request

这是Passport.js文件:

const passport = require('passport');
const router = require('express').Router();
//const UserController = require('../controller/UserController');

router
    // login
    .post("/login", passport.authenticate("local", function(req, res, next) {
        console.log(req);
    }))
    // logout
    .get('/logout', (req, res, next) => {
        req.session.destroy((err) => {
            res.redirect('/login')
        })
    })
    // signup
    .post("/signup", passport.authenticate("local-register"), (req, res) => {
        var userInfo = req.body;
    })

module.exports = router; 

我使用Postgresql作为此项目的数据库,使用pg-promise软件包。

这是app.js文件:

const bcrypt = require('bcrypt-nodejs');
const db = require("./queries");
const passport = require('passport');
const LocalStrategy = require("passport-local").Strategy

passport.use(new LocalStrategy(authenticate))
passport.use("local-register", new LocalStrategy({passReqToCallback: false}, register))

function authenticate(email, password, done) {
    db.one('select * from users where email = $1', email)
        .then((user) => {
            if(!user || !bcrypt.compareSync(password, user.password)) {
                return done(null, false, {message: "invalid user and password combination"});
            }
            done(null, user)
        }, done);
}

function register(email, password, done) {
    db.one('select * from users where email = $1', email)
        .then((user) => {
            if(user) {
                return done(null, false, {message: "An user with this address have already been created."});
            }
            const newUser = {
                email: email,
                password: bcrypt.hashSync(password),

            };
            db.none('insert into users(email, password) values($1, $2)', [newUser.email, newUser.password])
                .then((ids) => {
                    newUser.id = ids[0]
                    done(null, newUser)
                })
        })
}

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

passport.deserializeUser(function(id, done) {
    db.one('select * from users where id = $1', id)
        .then((user) => {
            done(null, user)
        }, done);
});

任何人都知道我为什么会有此回复?我尝试使用Insomnia上的电子邮件和密码的post方法,

谢谢!

2 个答案:

答案 0 :(得分:3)

默认情况下,LocalStrategy希望在名为username和password的参数中查找凭据。 (passport-local on Github)。

因此,您需要修改此行并将usernameField设置为email

passport.use("local-register", new LocalStrategy({passReqToCallback: false, usernameField: 'email'}, register))

答案 1 :(得分:0)

在第4行的passport.js文件中,确保结束第;

const LocalStrategy = require('passport-local');

您也可以单独验证您的详细信息

passport.use(new LocalStrategy((email, password, done) => {
 User.getUserByEmail(email, (err, email) => {
  if(err) throw err;
  if(!email){
    return done(null, false, {message: 'Incorrect Username/Email'});
  }

  User.comparePassword(password, user.password, (err, isMatch)=>{
    if(err) return done(err);
    if(isMatch){
      return done(null, email);
    } else {
      return done(null, false, {message: 'Invalid Password'});
    }
  });
})
}));

尝试坚持编写代码的一种方式,ES6或ES5。使用ES6时,我会建议您使用'use strict'帮助您彻底检查代码是否有错误。