Simplified local strategy signup process, email is now a username

This commit is contained in:
Sahat Yalkabov
2013-12-05 22:48:09 -05:00
parent 0c842286ef
commit 3ecd3ad148
3 changed files with 15 additions and 37 deletions

View File

@ -17,13 +17,10 @@ passport.deserializeUser(function(id, done) {
});
});
passport.use(new LocalStrategy({
usernameField: 'email'
},
function(email, password, done) {
User.findOne({ email: email }, function(err, user) {
passport.use(new LocalStrategy(function(username, password, done) {
User.findOne({ username: username }, function(err, user) {
if (err) return done(err);
if (!user) { return done(null, false, { message: 'Unknown user ' + email }); }
if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
user.comparePassword(password, function(err, isMatch) {
if (err) return done(err);
if(isMatch) {
@ -123,37 +120,27 @@ passport.use(new GoogleStrategy({
callbackURL: config.google.callbackUrl
},
function(accessToken, refreshToken, profile, done) {
console.log(accessToken);
console.log(profile);
User.findOne({ google: profile.id }, function(err, existingUser) {
if (err) {
done(err);
}
if (err) done(err);
if (existingUser) {
return done(null, existingUser);
}
var user = new User({
firstName: profile.name.givenName,
lastName: profile.name.familyName,
name: profile.displayName,
email: profile._json.email,
gender: profile._json.gender,
picture: profile._json.picture,
provider: profile.provider
});
user.tokens.google = accessToken;
user[profile.provider] = profile.id;
user.save(function(err) {
if (err) {
if (err.code === 11000) {
// Found another user with the same email
}
}
done(null, user);
done(err, user);
});
});
}
));

View File

@ -66,7 +66,7 @@ exports.getSignup = function(req, res) {
exports.postSignup = function(req, res) {
var user = new User({
email: req.body.email,
username: req.body.email,
password: req.body.password,
confirmPassword: req.body.confirmPassword
});

View File

@ -16,7 +16,7 @@ var userSchema = new mongoose.Schema({
provider: String,
facebook: String,
google: String,
isAdmin: Boolean,
isAdmin: Boolean
});
userSchema.path('password').validate(function(password) {
@ -24,20 +24,11 @@ userSchema.path('password').validate(function(password) {
return password.length;
}, 'Password cannot be blank');
userSchema.path('email').validate(function(email) {
userSchema.path('username').validate(function(username) {
if (this.provider) return true;
return email.length;
}, 'Email cannot be blank');
return username.length;
}, 'Username cannot be blank');
userSchema.path('firstName').validate(function(firstName) {
if (this.provider) return true;
return firstName.length;
}, 'First Name cannot be blank');
userSchema.path('lastName').validate(function(lastName) {
if (this.provider) return true;
return lastName.length;
}, 'Last Name cannot be blank');
userSchema.pre('save', function(next) {
var user = this;