Skip to content

Instantly share code, notes, and snippets.

@boudhayan-dev
Created December 25, 2019 12:12
Show Gist options
  • Save boudhayan-dev/c7eaf7ee5f9abaa63c53496aa864bebd to your computer and use it in GitHub Desktop.
Save boudhayan-dev/c7eaf7ee5f9abaa63c53496aa864bebd to your computer and use it in GitHub Desktop.
const mongoose = require('mongoose');
const bcrypt = require('bcrypt-nodejs');
var UserSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique:false
},
email:{
type: String,
required: true,
unique: true
},
password: {
type: String
},
created_on: {
type: Date,
default: Date.now
}
});
// Expand on LinksSchema
UserSchema.virtual('links', {
ref: 'Links',
localField: '_id',
foreignField: 'user'
})
UserSchema.methods.generateHash = function (password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
UserSchema.methods.validPassword = function (password) {
return bcrypt.compareSync(password, this.password);
};
UserSchema.set('toObject', { virtuals: true });
UserSchema.set('toJSON', { virtuals: true });
let User = mongoose.model('User', UserSchema);
module.exports = User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment