Skip to content

Commit

Permalink
models
Browse files Browse the repository at this point in the history
  • Loading branch information
Limebee authored Sep 28, 2022
1 parent 306c9a6 commit 988c52e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
18 changes: 18 additions & 0 deletions models/Todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mongoose = require('mongoose')

const TodoSchema = new mongoose.Schema({
todo: {
type: Array,
required: true,
},
completed: {
type: Boolean,
required: true,
},
userId: {
type: String,
required: true
}
})

module.exports = mongoose.model('Todo', TodoSchema)
36 changes: 36 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const bcrypt = require('bcrypt')
const mongoose = require('mongoose')

const UserSchema = new mongoose.Schema({
userName: { type: String, unique: true },
email: { type: String, unique: true },
password: String
})


// Password hash middleware.

UserSchema.pre('save', function save(next) {
const user = this
if (!user.isModified('password')) { return next() }
bcrypt.genSalt(10, (err, salt) => {
if (err) { return next(err) }
bcrypt.hash(user.password, salt, (err, hash) => {
if (err) { return next(err) }
user.password = hash
next()
})
})
})


// Helper method for validating user's password.

UserSchema.methods.comparePassword = function comparePassword(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
cb(err, isMatch)
})
}


module.exports = mongoose.model('User', UserSchema)

0 comments on commit 988c52e

Please sign in to comment.