Advertisement
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');
- // Define our model
- const userSchema = new Schema({
- email: { type: String, unique: true, lowercase: true},
- password: String,
- adress: String,
- age: String,
- firstname: String,
- lastname: String,
- city: String,
- travel: String
- });
- // On Save Hook, encrypt password
- // Before saving a model, run this function
- userSchema.pre('save', function(next){
- //get acess to the user model
- const user = this; // user.email, user.password
- //generate a salt then run callback
- bcrypt.genSalt(10, function(err, salt) {
- if (err) {return next(err);}
- // hash(encrypt) our password using the salt
- bcrypt.hash(user.password, salt, null, function(err, hash){
- if (err) {return next(err); }
- // overwrite plain text password with encrypted password
- user.password = hash;
- next();
- });
- });
- });
- userSchema.methods.comparePassword = function(candidatePassword, callback ){
- bcrypt.compare(candidatePassword, this.password, function(err, isMatch){
- if (err) { return callback(err); }
- callback(null, isMatch);
- });
- }
- // Create the model class
- const ModelClass = mongoose.model('user', userSchema);
- // Export the model
- module.exports = ModelClass;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement