Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require('mongoose');
- const Schema = mongoose.Schema;
- const bcrypt = require('bcrypt-nodejs');
- const UserSchema = new Schema({
- email : {
- type : String,
- unique : true
- },
- username: String,
- password: String,
- });
- UserSchema.pre('save', function (next) {
- var user = this;
- if (this.isModified('password') || this.isNew) {
- bcrypt.genSalt(10, function (err, salt) {
- if (err) {
- return next(err);
- }
- bcrypt.hash(user.password, salt, null, function (err, hash) {
- if (err) {
- return next(err);
- }
- user.password = hash;
- next();
- });
- });
- } else {
- return next();
- }
- });
- UserSchema.methods.comparePassword = function (pwd, cb) {
- bcrypt.compare(pwd, this.password, function (err, isMatch) {
- if (err) {
- return cb(err);
- }
- cb(null, isMatch);
- });
- };
- module.exports = mongoose.model('user', UserSchema);
Add Comment
Please, Sign In to add comment