1

I'm trying to set up very basic auth using jwt. I'm able to upload info directly to the mongodb cluster in postman, but I'm having a real rough time trying to have the same happen on the frontend.

When I go onto the "/register" on my localhost, type in the info and press submit, it responds with "name" is required

I am confused as I thought I am already submitting the name in the input text box. Here is my schema:

const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
    name:{
        type:String,
        required:true,
        max:300,
        min:6
    },
    email:{
        type:String,
        required:true,
        max:255,
        min:6
    },
    password:{
        type:String,
        required:true,
        max:1024,
        min:6
    },
    date:{
        type:Date,
        default:Date.now
    }
});

module.exports = mongoose.model('User',userSchema);

Here is posting process in my index.js

app.post('/register',async(req,res)=>{

    //Validation
    const{error} = registerValidation(req.body);
    if(error) return res.status(400).send(error.details[0].message);

    //If user already in db
    const emailExist = await User.findOne({email:req.body.email});
    if(emailExist) return res.status(400).send('Email already exists');

    //Salt generation
    const salt = await bcrypt.genSalt(10);
    const hashPassword = await bcrypt.hash(req.body.password,salt);

    //Create User
    const u = new User({
        name:req.body.name,
        email:req.body.email,
        password:hashPassword
    });
    try{
        const savedUser = await u.save();
        res.send(savedUser);
        //If want to send just id:
        //res.send({user:user._id});
    }catch(err){
        res.status(400).send(err);
    }
});

//LOGIN:
app.post('/login',async(req,res)=>{ 

    //Validation
    const{error} = loginValidation(req.body);
    if(error) return res.status(400).send(error.details[0].message);

    //If user already in db
    const u = await User.findOne({email:req.body.email});
    if(!u) return res.status(400).send("Email is wrong");

    //Password check
    const user_password = u.password;
    const password_history = req.body.password;
    if(user_password != password_history) return res.status(400).send("Password is wrong");

    //JWT 
    const token = jwt.sign({_id:u._id},process.env.TOKEN_SECRET);
    res.header('token',token).send(token);

});

Lastly here is the register form, register.ejs

<h1>Register</h1>
<form action="/register" method="POST">
  <div>
    <label for="name">Name</label>
    <input type="text" id="name" name="name" >
  </div>
  <div>
    <label for="email">Email</label>
    <input type="email" id="email" name="email" >
  </div>
  <div>
    <label for="password">Password</label>
    <input type="password" id="password" name="password" >
  </div>
  <button type="submit">Register</button>
</form>
<a href="/login">Login</a>

I am confused on why the server is stating that "name" is required. My understanding was that the submit button posts "/register" into the app.post(), which then requests a name, email and password, in accordance to the schema. I thought that name:req.body.name is the name that is being inputted as the name being used in registering the user. Can someone please explain why I am wrong?

For more context I put the whole project on plunkr: https://plnkr.co/edit/kd1kI4fZGGJnmarSh0pq?p=catalogue

Oscar
  • 23
  • 1
  • 6

1 Answers1

0

I figured it out. The problem was that I was submitting json data, not form-data. What I did to solve this problem was import body-parser, delete the line app.use(express.json());, and replaced it with app.use(bodyParser.urlencoded({ extended: false })). I checked my mongo cluster and lo and behold the user was registered into the database.

Oscar
  • 23
  • 1
  • 6