Skip to content

Commit

Permalink
instead of relying on previously hard-coded passport strategies we ca…
Browse files Browse the repository at this point in the history
…n assume that any user entry which have no provider set is a local strategy and apply our validations only on those empty provider members of the user model object
  • Loading branch information
lirantal committed Jan 12, 2014
1 parent ee63f17 commit 969c691
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
crypto = require('crypto'),
authTypes = ['github', 'twitter', 'facebook', 'google'];

crypto = require('crypto');

/**
* User Schema
Expand Down Expand Up @@ -49,25 +47,25 @@ var validatePresenceOf = function(value) {
// 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 (authTypes.indexOf(this.provider) !== -1) return true;
if (!this.provider) return true;
return name.length;
}, 'Name cannot be blank');

UserSchema.path('email').validate(function(email) {
// if you are authenticating by any of the oauth strategies, don't validate
if (authTypes.indexOf(this.provider) !== -1) return true;
if (!this.provider) return true;
return email.length;
}, 'Email cannot be blank');

UserSchema.path('username').validate(function(username) {
// if you are authenticating by any of the oauth strategies, don't validate
if (authTypes.indexOf(this.provider) !== -1) return true;
if (!this.provider) return true;
return username.length;
}, '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 (authTypes.indexOf(this.provider) !== -1) return true;
if (!this.provider) return true;
return hashed_password.length;
}, 'Password cannot be blank');

Expand All @@ -78,7 +76,7 @@ UserSchema.path('hashed_password').validate(function(hashed_password) {
UserSchema.pre('save', function(next) {
if (!this.isNew) return next();

if (!validatePresenceOf(this.password) && authTypes.indexOf(this.provider) === -1)
if (!validatePresenceOf(this.password) && !this.provider)
next(new Error('Invalid password'));
else
next();
Expand Down

0 comments on commit 969c691

Please sign in to comment.