fixed repeated login error, no longer throws duplicate user error
This commit is contained in:
@ -39,21 +39,23 @@ passport.use(new FacebookStrategy({
|
|||||||
callbackURL: config.facebook.callbackUrl || "http://localhost:8000/auth/facebook/callback"
|
callbackURL: config.facebook.callbackUrl || "http://localhost:8000/auth/facebook/callback"
|
||||||
},
|
},
|
||||||
function (accessToken, refreshToken, profile, done) {
|
function (accessToken, refreshToken, profile, done) {
|
||||||
User.findOne({ $where: profile.provider + '==' + profile.id }, function(err, existingUser) {
|
console.log(profile.provider);
|
||||||
if (existingUser) {
|
console.log(profile.id);
|
||||||
done(null, existingUser);
|
User.findOne({ facebook: profile.id }, function(err, existingUser) {
|
||||||
} else {
|
console.log(err);
|
||||||
|
console.log(existingUser);
|
||||||
|
if (existingUser) return done(null, existingUser);
|
||||||
var user = new User({
|
var user = new User({
|
||||||
firstName: profile.name.givenName,
|
firstName: profile.name.givenName,
|
||||||
lastName: profile.name.familyName,
|
lastName: profile.name.familyName,
|
||||||
provider: profile.provider
|
provider: profile.provider
|
||||||
});
|
});
|
||||||
user[profile.provider] = profile.id;
|
user[profile.provider] = profile.id;
|
||||||
|
|
||||||
user.save(function(err) {
|
user.save(function(err) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
done(null, user);
|
done(null, user);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,26 @@ var mongoose = require('mongoose'),
|
|||||||
bcrypt = require('bcrypt');
|
bcrypt = require('bcrypt');
|
||||||
|
|
||||||
var userSchema = new mongoose.Schema({
|
var userSchema = new mongoose.Schema({
|
||||||
|
email: { type: String, unique: true },
|
||||||
firstName: { type: String, required: true},
|
firstName: { type: String, required: true},
|
||||||
lastName: { type: String, required: true},
|
lastName: { type: String, required: true},
|
||||||
email: { type: String, required: true, unique: true },
|
password: String,
|
||||||
password: { type: String, required: true},
|
admin: { type: Boolean, default: false },
|
||||||
admin: { type: Boolean, default: false }
|
provider: String,
|
||||||
|
facebook: String,
|
||||||
|
google: String
|
||||||
});
|
});
|
||||||
|
|
||||||
|
userSchema.path('password').validate(function(password) {
|
||||||
|
if (this.provider) return true;
|
||||||
|
return password.length;
|
||||||
|
}, 'Password cannot be blank');
|
||||||
|
|
||||||
|
userSchema.path('email').validate(function(email) {
|
||||||
|
if (this.provider) return true;
|
||||||
|
return email.length;
|
||||||
|
}, 'Email cannot be blank');
|
||||||
|
|
||||||
userSchema.pre('save', function(next) {
|
userSchema.pre('save', function(next) {
|
||||||
var user = this;
|
var user = this;
|
||||||
var SALT_FACTOR = 5;
|
var SALT_FACTOR = 5;
|
||||||
|
Reference in New Issue
Block a user