forked from ipfs-service-provider

This commit is contained in:
Chris Troutner
2021-11-23 08:38:01 -08:00
commit 8f7f3896a3
115 changed files with 58101 additions and 0 deletions
+54
View File
@@ -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)