-2
해시 및 암호 해독을 시도하고 있지만 많은 오류가 있습니다! 해당 코드의 잘못된 점이나 올바르게 입력하는 방법은 무엇입니까?해싱 및 소금 절전 암호 입력란
의 user.js 코드
const mongoose = require('mongoose')
const schema = mongoose.Schema
const promise = require('bluebird')
const bcrypt = promise.promisifyAll(require('bcrypt'))
function hashPassword(user, option) {
const SALT_FACTOR = 8
if (!user.isModified('password')) {
return;
}
return bcrypt
.genSaltAsync(SALT_FACTOR)
.then(salt => bcrypt.hashAsync(user.password, salt, null))
.then(hash => {
user.setDataValue('password', hash)
})
}
// create schema and model
const userSchema = new schema({
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
}
})
userSchema.pre('create', function(next) {
hashPassword()
})
userSchema.pre('update', function(next) {
hashPassword()
})
userSchema.pre('save', function(next) {
hashPassword()
})
const user = mongoose.model('user', userSchema)
user.prototype.compairePassword = function (password) {
return bcrypt.compareAsync(password, this.password)
}
module.exports = user