Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require('mongoose')
- const bcrypt = require('bcrypt')
- const Schema = mongoose.Schema
- // let ObjectId = Schema.ObjectId
- let userSchema = new Schema({
- username: {type: String, unique: true},
- password: {type: String, required: true},
- email: {type: String, required: true},
- admin: Boolean,
- created_at: Date,
- updated_at: Date
- })
- userSchema.pre('save', function (next) {
- let user = this
- let currentDate = new Date()
- this.updated_at = currentDate
- if (!this.created_at) {
- this.created_at = currentDate
- }
- if (user.isModified('password')) {
- bcrypt.hashSync(user.password, 8)
- }
- next()
- })
- userSchema.statics.comparePassword = function (password, hash) {
- return new Promise((resolve) => {
- if (bcrypt.compareSync(password, hash)) {
- resolve(true)
- } else {
- resolve(false)
- }
- })
- }
- userSchema.statics.findByEmail = function (email, cb) {
- return this.find({email: email}, cb)
- }
- module.exports = mongoose.model('User', userSchema)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement