Simplified local strategy signup process, email is now a username
This commit is contained in:
@ -17,13 +17,10 @@ passport.deserializeUser(function(id, done) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
passport.use(new LocalStrategy({
|
passport.use(new LocalStrategy(function(username, password, done) {
|
||||||
usernameField: 'email'
|
User.findOne({ username: username }, function(err, user) {
|
||||||
},
|
|
||||||
function(email, password, done) {
|
|
||||||
User.findOne({ email: email }, function(err, user) {
|
|
||||||
if (err) return done(err);
|
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) {
|
user.comparePassword(password, function(err, isMatch) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
if(isMatch) {
|
if(isMatch) {
|
||||||
@ -123,37 +120,27 @@ passport.use(new GoogleStrategy({
|
|||||||
callbackURL: config.google.callbackUrl
|
callbackURL: config.google.callbackUrl
|
||||||
},
|
},
|
||||||
function(accessToken, refreshToken, profile, done) {
|
function(accessToken, refreshToken, profile, done) {
|
||||||
console.log(accessToken);
|
|
||||||
console.log(profile);
|
|
||||||
User.findOne({ google: profile.id }, function(err, existingUser) {
|
User.findOne({ google: profile.id }, function(err, existingUser) {
|
||||||
|
if (err) done(err);
|
||||||
if (err) {
|
|
||||||
done(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
return done(null, existingUser);
|
return done(null, existingUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
var user = new User({
|
var user = new User({
|
||||||
firstName: profile.name.givenName,
|
name: profile.displayName,
|
||||||
lastName: profile.name.familyName,
|
|
||||||
email: profile._json.email,
|
email: profile._json.email,
|
||||||
|
gender: profile._json.gender,
|
||||||
|
picture: profile._json.picture,
|
||||||
provider: profile.provider
|
provider: profile.provider
|
||||||
});
|
});
|
||||||
|
|
||||||
|
user.tokens.google = accessToken;
|
||||||
user[profile.provider] = profile.id;
|
user[profile.provider] = profile.id;
|
||||||
|
|
||||||
user.save(function(err) {
|
user.save(function(err) {
|
||||||
if (err) {
|
done(err, user);
|
||||||
if (err.code === 11000) {
|
|
||||||
// Found another user with the same email
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
done(null, user);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
@ -177,4 +164,4 @@ exports.ensureAdmin = function ensureAdmin(req, res, next) {
|
|||||||
else
|
else
|
||||||
res.send(403);
|
res.send(403);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -66,7 +66,7 @@ exports.getSignup = function(req, res) {
|
|||||||
exports.postSignup = function(req, res) {
|
exports.postSignup = function(req, res) {
|
||||||
|
|
||||||
var user = new User({
|
var user = new User({
|
||||||
email: req.body.email,
|
username: req.body.email,
|
||||||
password: req.body.password,
|
password: req.body.password,
|
||||||
confirmPassword: req.body.confirmPassword
|
confirmPassword: req.body.confirmPassword
|
||||||
});
|
});
|
||||||
|
@ -16,7 +16,7 @@ var userSchema = new mongoose.Schema({
|
|||||||
provider: String,
|
provider: String,
|
||||||
facebook: String,
|
facebook: String,
|
||||||
google: String,
|
google: String,
|
||||||
isAdmin: Boolean,
|
isAdmin: Boolean
|
||||||
});
|
});
|
||||||
|
|
||||||
userSchema.path('password').validate(function(password) {
|
userSchema.path('password').validate(function(password) {
|
||||||
@ -24,20 +24,11 @@ userSchema.path('password').validate(function(password) {
|
|||||||
return password.length;
|
return password.length;
|
||||||
}, 'Password cannot be blank');
|
}, 'Password cannot be blank');
|
||||||
|
|
||||||
userSchema.path('email').validate(function(email) {
|
userSchema.path('username').validate(function(username) {
|
||||||
if (this.provider) return true;
|
if (this.provider) return true;
|
||||||
return email.length;
|
return username.length;
|
||||||
}, 'Email cannot be blank');
|
}, '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) {
|
userSchema.pre('save', function(next) {
|
||||||
var user = this;
|
var user = this;
|
||||||
|
Reference in New Issue
Block a user