如何在用户模型中保存注册表单

时间:2018-04-17 07:05:21

标签: node.js mongodb express mongoose

我的注册页面上有很长的要求清单(公司名称,地址等),但我只需要注册路线的用户名和密码。如何在用户模型中保存注册页面中的所有项目而不干扰身份验证?

注册路线:

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

注册表单 所有条目数据都需要保存到数据库中以获取用户信息,但登录时只需要用户名和密码

<form action="/signup" method="POST">
          <div class="form-row">
            <div class="form-group col-md-12">
              <label for="inputCompany">Company:</label>
              <input type="text" class="form-control" name="company" placeholder="Company">
            </div>
            <div class="form-group col-md-12">
              <label for="inputAbn">ABN:</label>
              <input type="text" class="form-control" name="abn" placeholder="ABN">
            </div>
            <div class="form-group col-md-12">
              <label for="inputBuildingLicence">Building Licence:</label>
              <input type="text" class="form-control" name="lic" placeholder="Building Licence">
            </div>
            </div>
            <div class="form-group col-md-12">
                  <label for="inputName">Contact Name:</label>
                  <input type="text" class="form-control" name="contact" placeholder="Contact Name">
            </div>
            <div class="form-group col-md-12">
              <label for="inputContactNumber">Phone Number:</label>
              <input type="text" class="form-control" name="phone" placeholder="Contact Number">
            </div>
            <div class="form-group col-md-12">
              <label for="inputContactNumber">Mobile:</label>
              <input type="text" class="form-control" name="mobile" placeholder="Mobile Number">
            </div>
              <div class="form-group col-md-6">
                <label for="inputAddress">Address</label>
                <input type="text" class="form-control" name="addone" placeholder="1234 Main St">
              </div>
              <div class="form-group col-md-6">
                <label for="inputAddress2">Address 2</label>
                <input type="text" class="form-control" name="addtwo" placeholder="Apartment, studio, or floor">
              </div>

              <div class="form-row">
                <div class="form-group col-md-6">
                  <label for="inputCity">City</label>
                  <input type="text" class="form-control" name="city">
                </div>
                <div class="form-group col-md-4">
                  <label for="inputState">State</label>
                  <select name="state" class="form-control">
                    <option selected>Choose...</option>
                    <option>...</option>
                  </select>
                </div>
                <div class="form-group col-md-2">
                  <label for="inputZip">Zip</label>
                  <input type="text" class="form-control" name="zip">
                </div>
                <hr>
                <div class="form-row">
                    <div class="form-group col-md-12">
                      <label for="inputCompany">Email:</label>
                      <input type="text" class="form-control" name="username" placeholder="Email:">
                    </div>
                    <div class="form-group col-md-12">
                      <label for="inputAbn">Password:</label>
                      <input type="text" class="form-control" name="password" placeholder="Password">
                    </div>
                    <div class="form-group col-md-12">
                      <label for="inputAbn">Confirm the password:</label>
                      <input type="text" class="form-control" id="abn" placeholder="Confirm the Password">
                    </div>
              </div>
              <button type="submit" class="btn btn-primary btn-lg">submit</button>
    </form>

登录路线: 它应该使用来自User的用户名和密码登录:

app.post("/login", passport.authenticate("local",
   {
      successRedirect: "/myprojects",
      failureRedirect: "/login"
   }), function(req, res){
});

1 个答案:

答案 0 :(得分:1)

由于所有数据都来自同一个请求,因此您可以在注册过后保存用户数据。

在你的路线中:

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

            /* Here the registration is completed and the response is sent */
            user.state = req.body.state;
            user.city = req.body.city;

            user.save().catch( function( error ) {
                // No worries, the user is signed up already
                console.log( error );
            } ); 
        });

    });
});