使用护照使用NodeJs mongodb(mongoose)表达的注册表单

时间:2018-09-28 06:30:06

标签: javascript node.js mongoose passport.js

我正在使用包含用户名密码电子邮件之类的字段的注册表单,我正在使用mongodb存储这些数据,我的模式如下:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;
const passportLocalMongoose = require("passport-local-mongoose");
 var User = new Schema(
  {
    username: {type: String, required: true,unique: true },
    password: {type: String,required: true },
    email: { type: String, unique: true, required: true },
    admin: { type: Boolean, default: false }},
  { usePushEach: true},
  {timestamps: true }
);    
User.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", User);

在使用以下代码访问端点之前,我有一个中间件:

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('./models/user');

exports.local = passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

我有一个注册点,我已经写了这个:

router.post("/signup", (req, res, next) => {
  Users.register(
 new Users({ username: req.body.username,email:req.body.email }),
 req.body.password,

 function(err, account) {
   if (err) {
        res.statusCode = 500;
        res.setHeader("Content-Type", "application/json");
        res.json({ err: "err" });
      }
      passport.authenticate("local")(req, res, function() {
        res.send("/books");
      })})});

当更改后我通过用户名密码电子邮件传递json对象时,邮递员中出现此错误

{
    "err": {
        "errors": {
            "password": {
                "message": "Path `password` is required.",
                "name": "ValidatorError",
                "properties": {
                    "message": "Path `password` is required.",
                    "type": "required",
                    "path": "password"
                },
                "kind": "required",
                "path": "password",
                "$isValidatorError": true
            }
        },
        "_message": "User validation failed",
        "message": "User validation failed: password: Path `password` is required.",
        "name": "ValidationError"
    }
}

无论我是否遇到npm软件包问题,或者我必须处理任何回电,任何人都可以帮助我!

1 个答案:

答案 0 :(得分:1)

您似乎不想在架构中定义密码。护照当地猫鼬会帮您解决这个问题!

“您可以随意定义自己的用户。Passport-LocalMongoose将添加用户名,哈希和盐字段以存储用户名,哈希密码和盐值。” -https://github.com/saintedlama/passport-local-mongoose

尝试以下方法:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;
const passportLocalMongoose = require("passport-local-mongoose");
 var User = new Schema(
  {
    username: {type: String, required: true,unique: true },
    email: { type: String, unique: true, required: true },
    admin: { type: Boolean, default: false }},
  { usePushEach: true},
  {timestamps: true }
);    
User.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", User);

router.post('/signup', (req, res, next) => {
  Users.register(
    new Users({ username: req.body.username, email: req.body.email}),
    req.body.password,
    function(err, account) {
      if (err) {
        res.statusCode = 500;
        res.setHeader('Content-Type', 'application/json');
        res.json({ err: 'err' });
      }
      passport.authenticate('local')(req, res, function() {
        res.send('/books');
      });
    }
  );
});

让我知道这是否可以解决您的问题。