mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-consumer.git
synced 2026-09-21 16:52:03 -07:00
51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
import mongoose from 'mongoose'
|
|
import bcrypt from 'bcrypt'
|
|
|
|
const User = new mongoose.Schema({
|
|
type: { type: String, default: 'User' },
|
|
name: { type: String },
|
|
username: { type: String, required: true, unique: true },
|
|
password: { type: String, required: true },
|
|
salt: { type: String }
|
|
})
|
|
|
|
User.pre('save', function preSave(next) {
|
|
const user = this
|
|
|
|
if (!user.isModified('password')) {
|
|
return next()
|
|
}
|
|
|
|
new Promise((resolve, reject) => {
|
|
bcrypt.genSalt(10, (err, salt) => {
|
|
if (err) { return reject(err) }
|
|
resolve(salt)
|
|
})
|
|
})
|
|
.then(salt => {
|
|
bcrypt.hash(user.password, salt, (err, hash) => {
|
|
if (err) { throw new Error(err) }
|
|
|
|
user.password = hash
|
|
user.salt = salt
|
|
|
|
next(null)
|
|
})
|
|
})
|
|
.catch(err => next(err))
|
|
})
|
|
|
|
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) }
|
|
|
|
resolve(isMatch)
|
|
})
|
|
})
|
|
}
|
|
|
|
export default mongoose.model('user', User)
|