Reputation: 103
If user doesn't accept the term policy checkbox, I don't want mongoose and node.js to register this user in server side.
Front end checkbox
<div className="mb-3 form-check">
<input type="checkbox" className="form-check-input register-input" name="registerCheckbox" id="exampleCheck1" onChange={e => this.checkController(e)} />
<label className="form-check-label" htmlFor="exampleCheck1">I agree the non-existing terms of use.</label>
</div>
Server side and what I did
const User = require('../Models/User')
const errHandler = require('express-async-handler')
const path = require('path')
const registerUser = errHandler(async (req, res) => {
const { name, nickName, email, password, role, perk, registerCheckbox } = req.body;
const user = await User.create({
name,
nickName,
password,
email,
role,
perk
})
if (!registerCheckbox) {
res.status(400)
.json({
success:false
})
}
res.status(201)
.sendFile(path.resolve('./Views/register.html'))
})
module.exports = {
registerUser
}
Even if it throws success false, I can't prevent to submit new registration and when I check DB, I see that it creates the new registration.
How can I prevent this situation in server side ? I am new in back end.
Upvotes: 0
Views: 42
Reputation: 5411
You need to check if registerCheckbox
is true before save the user in the database. It should be:
const User = require('../Models/User')
const errHandler = require('express-async-handler')
const path = require('path')
const registerUser = errHandler(async (req, res) => {
const { name, nickName, email, password, role, perk, registerCheckbox } = req.body;
// perform the verification
if (!registerCheckbox) {
res.status(400)
.json({
success:false
});
return; // I added the return here to stop the function when the verification is failed
}
// success case, add user in database
const user = await User.create({
name,
nickName,
password,
email,
role,
perk
})
res.status(201)
.sendFile(path.resolve('./Views/register.html'))
})
module.exports = {
registerUser
}
Upvotes: 1