aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/models/user.js
diff options
context:
space:
mode:
authorDennis Eriksen <dennis.se@gmail.com>2013-04-29 03:39:15 +0200
committerDennis Eriksen <dennis.se@gmail.com>2013-04-29 03:39:15 +0200
commitfc50b5b7f18d02ffa8056dc1edbd3bd43e973493 (patch)
treee4646512634a42790bbddfec8b3d035bb2052cec /models/user.js
parentadded twitter authentication (diff)
downloadDivid-fc50b5b7f18d02ffa8056dc1edbd3bd43e973493.tar.gz
been cleaning a bit, moving stuff to where it belongs. signup and
signin works.
Diffstat (limited to 'models/user.js')
-rw-r--r--models/user.js128
1 files changed, 128 insertions, 0 deletions
diff --git a/models/user.js b/models/user.js
new file mode 100644
index 0000000..b61f7c9
--- /dev/null
+++ b/models/user.js
@@ -0,0 +1,128 @@
+
+/**
+ * Module dependencies
+ */
+
+var mongoose = require('mongoose')
+ , Schema = mongoose.Schema
+ , crypto =require('crypto')
+ , authTypes = ['facebook', 'twitter'];
+
+
+/**
+ * User schema
+ */
+
+var UserSchema = new Schema({
+ name: String,
+ email: String,
+ username: String,
+ provider: String,
+ hashed_password: String,
+ salt: String,
+ facebook: {},
+ twitter: {}
+});
+
+/**
+ * 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 validatePrecenceOf = function (value) {
+ return value && value.length;
+}
+
+// the four validations below only apply if you are signing up traditionally
+
+UserSchema.path('name').validate(function(name) {
+ // if you're authenticated by any of the oauth strategies (facebook, twitter), don't validate
+ if(authTypes.indexOf(this.provider) !== -1) return true;
+ return name.length;
+}, 'Name cannot be blank');
+
+UserSchema.path('email').validate(function(email) {
+ if(authTypes.indexOf(this.provider) !== -1) return true;
+ return email.length;
+}, 'Email cannot be blank');
+
+UserSchema.path('username').validate(function(username) {
+ if(authTypes.indexOf(this.provider) !== -1) return true;
+ return username.length;
+}, 'Username cannot be blank');
+
+UserSchema.path('hashed_password').validate(function(hashed_password) {
+ if(authTypes.indexOf(this.provider) !== -1) return true;
+ return hashed_password.length;
+}, 'Password cannot be blank');
+
+/**
+ * Pre-save hook
+ */
+
+UserSchema.pre('save', function(next) {
+ if (!this.isNew) return next();
+
+ if(!validatePrecenceOf(this.password)
+ && authTypes.indexOf(this.provider) === -1)
+ next(new Error('Invalid password'));
+ else next();
+});
+
+
+/**
+ * Methods
+ */
+
+UserSchema.methods = {
+
+ /**
+ * Authenticate - check if passwords are the same
+ *
+ * @param {String} plainText
+ * @return {Bolean}
+ * @api public
+ */
+
+ authenticate: function(plainText) {
+ return this.encryptPassword(plainText) === this.hashed_password;
+ },
+
+ /**
+ * Make salt
+ *
+ * @return {String}
+ * @api public
+ */
+
+ makeSalt: function() {
+ return Math.round((new Date().valueOf() * Math.random())) + '';
+ },
+
+ /**
+ * Encrypt password
+ *
+ * @param {String} password
+ * @return {String}
+ * @api public
+ */
+
+ encryptPassword: function(password) {
+ if (!password) return '';
+ return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
+ }
+}
+
+mongoose.model('User', UserSchema);
+
+