Skip to content

Instantly share code, notes, and snippets.

@thebigredgeek
Last active December 25, 2015 07:59
Show Gist options
  • Save thebigredgeek/6943195 to your computer and use it in GitHub Desktop.
Save thebigredgeek/6943195 to your computer and use it in GitHub Desktop.
Facebook passport
var passport = require('passport'),
FacebookStrategy = require('passport-facebook').Strategy;
/**
* This function handles passport verification
* @param {Object} token Token
* @param {Object} tokenSecret Token secret
* @param {Object} profile Information regarding this user
* @param {Function} done Callback to execute upon completion
*/
var verifyHandler = function (token, tokenSecret, profile, done) {
process.nextTick(function(){ //handle on the next cycle
console.log("Verifying...");
Users.findOne({uid: profile.id}).done(function(err, user){
if(user){ //If this is an already-valid user
return done(null, user); //We are finished!
}
else{ //If this is not an already-valid user
Users.create({
provider : profile.provider, //pass the provider
uid : profile.id, //pass the user id
name : profile.displayName //pass the display name
}).done(function(err, user){
return done(err, user); //Return eror
});
}
});
});
};
/**
* This function serializes user data to send back to the suer
* @param {Object} user User object
* @param {Function} done Callback
*/
passport.serializeUser(function(user, done){
done(null, user.uid);
});
/**
* This function deserializes the user data so that we can look up the user
* @param {String} uid The user's id
* @param {Function} done Callback
*/
passport.deserializeUser(function(uid, done){
Users.findOne({uid: uid}).done(function(err, user){
done(err, user)
});
});
passport.use(new FacebookStrategy({
clientID : "xxxxxxxxxxxxxxxxxxxxxx",
clientSecret : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
callbackURL : "http://127.0.0.1:1337/auth/facebook/callback"
},verifyHandler));
module.exports.express = {
customMiddleware: function(app){
console.log("passport");
app.use(passport.initialize());
app.use(passport.session());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment