Whenever I try to register with the same email on the registration page, it still gets recorded in my MongoDB database when I don't expect it to be saved.
I attempted to set 'unique: true' for the email variable in User.js like this:
import bcrypt from 'bcrypt';
import { Schema, model, models } from "mongoose";
const UserSchema = new Schema({
email: {type: String, required: true, unique: true},
password: {
type: String,
required: true,
validate: pass => {
if(!pass?.length || pass.length < 5){
new Error('Password must be at least 5 characters long');
return false;
}
},
},
},{timestamps: true});
UserSchema.post('validate', function (user){
const notHashedPassword = user.password;
const salt = bcrypt.genSaltSync(10);
user.password = bcrypt.hashSync(notHashedPassword, salt);
})