12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- var bcrypt = require('bcrypt-nodejs');
- var crypto = require('crypto');
- var mongoose = require('mongoose');
- var userSchema = new mongoose.Schema({
- email: { type: String, unique: true, lowercase: true },
- password: String,
- tokens: Array,
- profile: {
- name: { type: String, default: '' },
- website: { type: String, default: '' },
- picture: { type: String, default: '' }
- },
- resetPasswordToken: String,
- resetPasswordExpires: Date
- });
- /********** PASS HASH **************/
- userSchema.pre('save', function(next) {
- var user = this;
- if (!user.isModified('password')) {
- return next();
- }
- 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();
- });
- });
- });
- /********** PASS Valid **************/
- userSchema.methods.comparePassword = function(candidatePassword, cb) {
- bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
- if (err) {
- return cb(err);
- }
- cb(null, isMatch);
- });
- };
- module.exports = mongoose.model('User', userSchema);
|