Files
meanTorrent/app/models/user.js
2014-01-25 12:03:26 -06:00

125 lines
3.0 KiB
JavaScript
Executable File

'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
crypto = require('crypto');
/**
* User Schema
*/
var UserSchema = new Schema({
name: String,
email: String,
username: {
type: String,
unique: true
},
hashed_password: String,
provider: String,
salt: String,
facebook: {},
twitter: {},
github: {},
google: {},
linkedin: {}
});
/**
* Virtuals
*/
UserSchema.virtual('password').set(function(password) {
this._password = password;
this.salt = this.makeSalt();
this.hashed_password = this.encryptPassword(password);
}).get(function() {
return this._password;
});
/**
* Validations
*/
var validatePresenceOf = function(value) {
return value && value.length;
};
// the below 4 validations only apply if you are signing up traditionally
UserSchema.path('name').validate(function(name) {
// if you are authenticating by any of the oauth strategies, don't validate
if (!this.provider) return true;
return (typeof name === 'string' && name.length > 0);
}, 'Name cannot be blank');
UserSchema.path('email').validate(function(email) {
// if you are authenticating by any of the oauth strategies, don't validate
if (!this.provider) return true;
return (typeof email === 'string' && email.length > 0);
}, 'Email cannot be blank');
UserSchema.path('username').validate(function(username) {
// if you are authenticating by any of the oauth strategies, don't validate
if (!this.provider) return true;
return (typeof username === 'string' && username.length > 0);
}, 'Username cannot be blank');
UserSchema.path('hashed_password').validate(function(hashed_password) {
// if you are authenticating by any of the oauth strategies, don't validate
if (!this.provider) return true;
return (typeof hashed_password === 'string' && hashed_password.length > 0);
}, 'Password cannot be blank');
/**
* Pre-save hook
*/
UserSchema.pre('save', function(next) {
if (!this.isNew) return next();
if (!validatePresenceOf(this.password) && !this.provider)
next(new Error('Invalid password'));
else
next();
});
/**
* Methods
*/
UserSchema.methods = {
/**
* Authenticate - check if the passwords are the same
*
* @param {String} plainText
* @return {Boolean}
* @api public
*/
authenticate: function(plainText) {
return this.encryptPassword(plainText) === this.hashed_password;
},
/**
* Make salt
*
* @return {String}
* @api public
*/
makeSalt: function() {
return crypto.randomBytes(16).toString('base64');
},
/**
* Encrypt password
*
* @param {String} password
* @return {String}
* @api public
*/
encryptPassword: function(password) {
if (!password || !this.salt) return '';
var salt = new Buffer(this.salt, 'base64');
return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64');
}
};
mongoose.model('User', UserSchema);