I want to understand some best practices about handling user login and sign up requests.
For example, if the user tries to sign up with their email, and their account already exists, what should happen? Should my server reject the request? Or should I fulfill the request with some status code, and handle it on client side?
Currently, my code below will fulfill the request and send a custom message and i handle it from the client
app.post('/signup', (req, res) => {
const email = req.body.email
const plainTextPassword = req.body.password;
//check if user already exists
User.find({ email: email }, (err, existingUser) => {
//account doesnt exist
if (existingUser.length === 0) {
bcrypt.hash(plainTextPassword, saltRounds, async (err, hash) => {
try {
const user = new User({
email: email,
password: hash
});
let result = await user.save();
if (result) {
res.send(result)
}
} catch (e) {
res.send(e);
}
})
} else {
//notify user that account exists
res.json("account exists")
}
})
})
where can i read more about this type of practices? thanks