Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const bcrypt = require('bcrypt-nodejs');
- const mongoose = require('mongoose');
- const userSchema = new mongoose.Schema({
- email: {
- type: String,
- required: true,
- 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, null, (err, hash) => {
- if (err) { return next(err); }
- user.password = hash;
- next();
- });
- });
- });
- /**
- * Helper method for validating user's password.
- */
- userSchema.methods.comparePassword = function comparePassword(candidatePassword) {
- return new Promise((resolve, reject) => {
- bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
- if (err) { reject(err); }
- resolve(isMatch);
- });
- });
- };
- const User = mongoose.model('User', userSchema);
- module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement