정승호

비밀번호 암호화 작업

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const saltRounds = 10;
const userSchema = mongoose.Schema({
name : {
type : String,
......@@ -31,6 +33,27 @@ const userSchema = mongoose.Schema({
})
// password 암호화
userSchema.pre('save', function(next){
var user = this;
if(user.isModified('password')){
bcrypt.genSalt(saltRounds, function(err, salt){
if(err)return next(err)
bcrypt.hash(user.password, salt, function(err, hash){
if(err) return next(err)
user.password = hash
//hash 값으로 변경해서 저장
next()
})
})
}
})
const User = mongoose.model('Users', userSchema)
......
This diff is collapsed. Click to expand it.
......@@ -15,6 +15,7 @@
"author": "seungho",
"license": "ISC",
"dependencies": {
"bcrypt": "^4.0.1",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mongoose": "^5.9.15"
......