mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-21 16:52:00 -07:00
Use promises for flatter code
This commit is contained in:
+18
-11
@@ -9,36 +9,43 @@ const User = new mongoose.Schema({
|
||||
salt: { type: String }
|
||||
})
|
||||
|
||||
User.pre('save', function(next) {
|
||||
User.pre('save', function preSave(next) {
|
||||
const user = this
|
||||
|
||||
if(!user.isModified('password')) {
|
||||
if (!user.isModified('password')) {
|
||||
return next()
|
||||
}
|
||||
|
||||
bcrypt.genSalt(10, (err, salt) => {
|
||||
if(err) { return next(err) }
|
||||
|
||||
new Promise((resolve, reject) => {
|
||||
bcrypt.genSalt(10, (err, salt) => {
|
||||
if (err) { return reject(err) }
|
||||
return salt
|
||||
})
|
||||
})
|
||||
.then(salt => {
|
||||
bcrypt.hash(user.password, salt, (err, hash) => {
|
||||
if(err) { return next(err) }
|
||||
if (err) { throw new Error(err) }
|
||||
|
||||
user.password = hash
|
||||
user.salt = salt
|
||||
next()
|
||||
return user
|
||||
})
|
||||
})
|
||||
.then(() => {
|
||||
next()
|
||||
})
|
||||
})
|
||||
|
||||
User.methods.validatePassword = function(password) {
|
||||
User.methods.validatePassword = function validatePassword(password) {
|
||||
const user = this
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
bcrypt.compare(password, user.password, (err, isMatch) => {
|
||||
if(err) { return reject(err) }
|
||||
if (err) { return reject(err) }
|
||||
|
||||
resolve(isMatch)
|
||||
});
|
||||
})
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
export default mongoose.model('user', User)
|
||||
|
||||
Reference in New Issue
Block a user