Reputation: 121
I am having a signup form with fields like username password email, i am using mongodb to store these data my schema looks like:
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
{
"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!
Upvotes: 0
Views: 748
Reputation: 1316
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." - 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.
Upvotes: 1