Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- const mongoose = require('mongoose')
- mongoose.Promise = global.Promise
- const Schema = mongoose.Schema
- const bcrypt = require('bcrypt-nodejs')
- const userSchema = Schema({
- email: { type: String, unique: true, lowercase: true },
- displayName: String,
- avatar: String, //url donde se almacena
- password: { type: String, select:false } // para que al hacer consultas no lo envíe al cliente
- signupDate: { type: Date, default: Date.now() }
- lastLogin: Date
- })
- // Aquí aplica hash en el evento findOne
- userSchema.pre('findOne', (next) => {
- let user = this
- bcrypt.genSalt(10, (err, salt) => {
- if (err) return next(err)
- bcrypt.hash(user.password, salt, null, (err, hash) => {
- if(err) return next(err)
- user.password = hash
- console.log(user.password)
- next()
- })
- })
- })
- let usu = new Usuario({
- usuario : req.body.usuario,
- password : req.body.password
- })
- Usuario.findOne( {usu_nombre: usu.usuario}, (err, user) => {
- console.log(usu.password)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement