Created
January 4, 2012 22:43
-
-
Save rockwood/1562615 to your computer and use it in GitHub Desktop.
Everyauth findUserById
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var UserSchema = new Schema({ | |
email : String, | |
hashed_password : String, | |
first_name : String, | |
last_name : String, | |
salt : String, | |
auth_method : String, | |
signupDate : { type: Date, default: Date.now } | |
}); | |
var User = mongoose.model('User', UserSchema); | |
UserSchema.plugin(mongooseAuth, { | |
everymodule: { | |
everyauth: { | |
User: function () { | |
return User; | |
}, | |
findUserById: function (userId, callback) { | |
console.log('findUserById') | |
User.findById(userId, function(err, user){ | |
if(err) | |
callback(err, null) | |
callback(null, user); | |
}); | |
} | |
}, | |
}, | |
password: { | |
loginWith: 'email', | |
everyauth: { | |
getLoginPath: '/sessions/new', | |
postLoginPath: '/sessions', | |
loginView: 'sessions/new', | |
getRegisterPath:'/users/new', | |
postRegisterPath: '/users', | |
registerView: 'users/new', | |
loginSuccessRedirect: '/dashboard', | |
registerSuccessRedirect: '/dashboard', | |
extractExtraRegistrationParams: function (req) { | |
return{ | |
first_name: req.body.first_name, | |
last_name: req.body.last_name, | |
password_confirm: req.body.password_confirm | |
} | |
}, | |
validateRegistration: function (newUserAttrs, errors) { | |
if(newUserAttrs.password != newUserAttrs.password_confirm){ | |
errors.push(new Error("passwords dont match")); | |
} | |
return errors; | |
}, | |
registerUser: function (newUserAttrs) { | |
delete newUserAttrs.password_confirm; | |
var promise = this.Promise(); | |
User.createWithPassword(newUserAttrs, function(err, user) { | |
if (err){ | |
return promise.fulfill([err]); | |
} | |
promise.fulfill(user); | |
}); | |
return promise; | |
}, | |
authenticate: function (email, password) { | |
console.log('authenticating'); | |
var promise = this.Promise(); | |
User.authenticate(email, password, function(err, user) { | |
if (err){ | |
return promise.fulfill([err]); | |
} | |
return promise.fulfill(user); | |
}); | |
return promise; | |
} | |
} | |
}, | |
facebook: { | |
everyauth: { | |
appId: process.env.facebookAppId || conf.facebook.appId, | |
appSecret: process.env.facebookappSecret || conf.facebook.appSecret, | |
redirectPath: '/dashboard', | |
scope: 'email', | |
findOrCreateUser: function (session, accessToken, accessTokenExtra, facebookUserData) { | |
var promise = this.Promise(), | |
User = this.User()(); | |
User.findById(facebookUserData.id, function (err, user) { | |
if (!user) { | |
console.log('looking up user', facebookUserData.email); | |
User.findOne( { 'email': facebookUserData.email }, function (err, user) { | |
if (!user) { | |
User.createWithFacebook(facebookUserData, accessToken, accessTokenExtra.expires, function (err, user) { | |
if (err) | |
return promise.fail(err); | |
return promise.fulfill(user); | |
}); | |
} else { | |
User.updateFacebookData(user, accessToken, accessTokenExtra, facebookUserData); | |
user.save( function (err, user) { | |
if (err) | |
return promise.fail(err); | |
promise.fulfill(user); | |
}); | |
} | |
return promise.fulfill(user); | |
}); | |
} | |
}); | |
return promise; | |
}, | |
handleAuthCallbackError: function (req, res) { | |
req.flash('error', "<b>Oops!</b>Facebook sign in failed") | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment