Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const passwordValidators = [
- {
- validator: passwordLengthChecker,
- message: 'Password must be at least 5 characters but no more than 40'
- },
- {
- validator:validPassword,
- message: 'Must have at least one uppercase, lowercase, special character, and number'
- }
- ];
- /* ========
- Schema for user
- ========= */
- const userSchema=new Schema({
- email: { type: String, required: true, unique: true, lowercase: true, validate: emailValidators},
- username: { type: String, required: true, unique: true, lowercase: true, validate: usernameValidators},
- password: { type: String, required: true,validate: passwordValidators},
- bio: { type:String,default:null},
- location: {type:String, default:null},
- gender: {type:String,default:null},
- birthday: { type:Date,default:null},
- img: { type:String, default:null}
- });
- // Middleware that encrypt password
- userSchema.pre('save',function(next){
- if(!this.isModified('password'))
- return next();
- bcrypt.hash(this.password, null, null, (err,hash)=>{
- if(err) return next(err);
- this.password=hash;
- next();
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement