0
votes

I am trying to save a User and Register it at the same time. I am having troubles doing both simultaneously.
My current Schema:

const userSchema = new mongoose.Schema({
username: String,
password: String,
firstName: String,
lastName: String,
player: {
  type: Boolean,
  default: true
  }
});
userSchema.plugin(passportLocalMongoose);
module.exports = (User, "userSchema");

My current form:

<form action="/player" method="POST">
  <input type="text" name="user[username]" placeholder="username">
  <input type="password" name ="user[password]" placeholder="password">
  <input type="text" name="user[firstName]" placeholder="First Name">
  <input type="text" name="user[lastName]" placeholder="Last Name">
  <button>Submit</button>
</form>

I am requiring in express, passport, passport-local, passport-local-mongoose, body-parser, mongoose, and my schemas.

My app.js after that is:

mongoose.connect(database);
app.use(bodyParser.urlencoded({extended: true}));
app.use(require("express-session")({
  secret: "Generic Secret Password",
  resave: false,
  saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serialUser());
passport.deserializeUser(User.deserializeUser());

//route
app.post("/player", function(req, res){
var newUser = new User({ username: req.body.user.email });
User.register(newUser, req.body.user.password, function(err, user){
    if(err){
        console.log(err);
        return res.render("new");
    } 
    passport.authenticate("local")(req, res, function(){
        res.redirect("/player");
    });
});

The problem I am having is that when I submit, the User saves the salt, hash, username, and sets player to "true", but it doesn't save lastName or firstName and it follows with a bad request(400). I can do a User.Create and it saves everything (minus the hash and salt) and redirects fine. I can also set the form up so that "user[username]" and "user[password]" are "username" and "password" and get it to save everything but firstName and lastName and it will redirect properly without a 400 status. I have tried changing the default inputs for "usernameField" and "passwordField", but I have had no luck. I've tried to look at various SO posts and the closest I found was a similar suggestion to change the defaults, so maybe I am doing it wrong. I've also tried to redirect to different routes as well and still no luck.

This is the first project I've worked on that isn't a code-along and I've tried to be as specific as possible on everything and I've been struggling for a solid eight-hours. Thanks for any help!

2

2 Answers

0
votes

You have to specify the different parameters

var newUser = new User({ username: req.body.user.email, 
                         firstName: req.body.user.firstName, 
                         lastName: req.body.user.lastName});
0
votes

you have to specify first name and lastname here var newUser = new User({ username: req.body.user.email, firstname: req.body.user.firstname, lastname:req.body.user.lastname});