[英]SignUp form with NodeJs mongodb(mongoose) express using passport
I am having a signup form with fields like username password email, i am using mongodb to store these data my schema looks like: 我正在使用包含用户名密码电子邮件之类的字段的注册表单,我使用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);
i have a middleware before hitting the endpoint with the following code: 在使用以下代码访问端点之前,我有一个中间件:
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());
i have an end point to hit signup and i have written this : 我有一个结束注册的终点,我已经写了这个:
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");
})})});
when i pass the json object with username password email after the changes i got this error in postman 当我在更改后通过用户名密码电子邮件传递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"
}
}
can anyone help me in this whether i am having issue with npm package or is there any call back i have to handle,I cant figure out! 任何人都可以在这方面帮助我,无论我是否遇到npm软件包问题,或者我必须处理任何回电,我都无法弄清楚!
It looks like you don't want to define the password in your schema. 您似乎不想在架构中定义密码。 Passport-local-mongoose will take care of that for you! 护照当地猫鼬会帮您解决这个问题!
"You're free to define your User how you like. Passport-Local Mongoose will add a username, hash and salt field to store the username, the hashed password and the salt value." “您可以随意定义自己的用户。Passport-LocalMongoose将添加一个用户名,哈希和盐字段以存储用户名,哈希密码和salt值。” - https://github.com/saintedlama/passport-local-mongoose -https://github.com/saintedlama/passport-local-mongoose
Try this instead: 尝试以下方法:
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');
});
}
);
});
Let me know if that solves your problem. 让我知道是否可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.