ayrtonteshima / ead-api

API da plataforma de ensino a distância desenvolvida junto ao canal do YouTube Programador a Bordo
MIT License
196 stars 34 forks source link

Fazer o Hash da senha diretamente no model #21

Open fabiomattes2016 opened 4 years ago

fabiomattes2016 commented 4 years ago
const mongoose = require('mongoose');
const { Schema } = mongoose
const UUIDv4 = require('uuid').v4();
const bcrypt = require('bcryptjs');

const UserSchema = new Schema({
  _id: {
    type: String,
    default: UUIDv4,
  },
  name: String,
  dateOfBirth: Date,
  docType: String,
  docNumber: String,
  email: String,
  status: Number,
  password: String,
  address: {
    street: String,
    complement: String,
    country: String,
    state: String,
    city: String,
    zipcode: String,
    number: String,
  },
}, {
  timestamps: {}
});

UserSchema.pre('save', async function(next) {
  const hash = await bcrypt.hash(this.password, 10);
  this.password = hash;

  next();
});

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