Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Merge branch 'enhancement-authtypes_refactor' of https://github.com/l…
Browse files Browse the repository at this point in the history
…irantal/mean into lirantal-enhancement-authtypes_refactor
  • Loading branch information
liorkesos committed Jan 20, 2014
2 parents a5410c0 + 3963892 commit 526fc0a
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 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,26 +47,26 @@ 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;
return name.length;
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 (authTypes.indexOf(this.provider) !== -1) return true;
return email.length;
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 (authTypes.indexOf(this.provider) !== -1) return true;
return username.length;
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 (authTypes.indexOf(this.provider) !== -1) return true;
return hashed_password.length;
if (!this.provider) return true;
return (typeof hashed_password === 'string' && hashed_password.length > 0);
}, '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 526fc0a

Please sign in to comment.