UNPKG

react-hackathon-board

Version:
84 lines (70 loc) 2.1 kB
import bcrypt from 'bcrypt-nodejs'; import crypto from 'crypto'; import mongoose from 'mongoose'; import Promise from 'bluebird'; mongoose.Promise = Promise; var userSchema = new mongoose.Schema({ username: String, email: { type: String, unique: true, lowercase: true }, password: String, facebook: String, twitter: String, google: String, github: String, linkedin: String, tokens: Array, lockedAt: Date, authFailedCount: { type: Number, default: 0 }, judge: Boolean, selectedHackathon: String, profile: { name: { type: String, default: '' }, gender: { type: String, default: '' }, location: { type: String, default: '' }, website: { type: String, default: '' }, picture: { type: String, default: '' }, avatar: { type: String, default: ''}, description: { type: String, default: ''} }, resetPasswordToken: String, resetPasswordExpires: Date }); /** * Password hash middleware. */ userSchema.pre('save', function(next) { var user = this; if (!user.isModified('password')) { console.log("password was NOT modified"); return next(); } console.log("password was modified, salting it..."); 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(); }); }); }); /** * Helper method for validating user's password. */ userSchema.methods.comparePassword = function(candidatePassword, cb) { bcrypt.compare(candidatePassword, this.password, function(err, isMatch) { if (err) return cb(err); cb(null, isMatch); }); }; /** * Helper method for getting user's gravatar. */ userSchema.methods.gravatar = function(size) { if (!size) size = 200; if (!this.email) return 'https://gravatar.com/avatar/?s=' + size + '&d=retro'; var md5 = crypto.createHash('md5').update(this.email).digest('hex'); return 'https://gravatar.com/avatar/' + md5 + '?s=' + size + '&d=retro'; }; var User = mongoose.model('User', userSchema); export default User;