mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-consumer.git
synced 2026-09-21 16:52:03 -07:00
forked from ipfs-service-provider
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
const mongoose = require('mongoose')
|
||||
const bcrypt = require('bcryptjs')
|
||||
const config = require('../../../../config')
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const User = new mongoose.Schema({
|
||||
type: { type: String, default: 'user' },
|
||||
name: { type: String },
|
||||
username: { type: String },
|
||||
password: { type: String, required: true },
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
}
|
||||
})
|
||||
|
||||
// Before saving, convert the password to a hash.
|
||||
User.pre('save', async function preSave (next) {
|
||||
const user = this
|
||||
|
||||
if (!user.isModified('password')) {
|
||||
return next()
|
||||
}
|
||||
|
||||
const salt = await bcrypt.genSalt(10)
|
||||
const hash = await bcrypt.hash(user.password, salt)
|
||||
|
||||
user.password = hash
|
||||
|
||||
next(null)
|
||||
})
|
||||
|
||||
// Validate the password by comparing to the saved hash.
|
||||
User.methods.validatePassword = async function validatePassword (password) {
|
||||
const user = this
|
||||
|
||||
const isMatch = await bcrypt.compare(password, user.password)
|
||||
|
||||
return isMatch
|
||||
}
|
||||
|
||||
// Generate a JWT token.
|
||||
User.methods.generateToken = function generateToken () {
|
||||
const user = this
|
||||
|
||||
const token = jwt.sign({ id: user.id }, config.token)
|
||||
// console.log(`config.token: ${config.token}`)
|
||||
// console.log(`generated token: ${token}`)
|
||||
return token
|
||||
}
|
||||
|
||||
// export default mongoose.model('user', User)
|
||||
module.exports = mongoose.model('user', User)
|
||||
Reference in New Issue
Block a user