Skip to content

Instantly share code, notes, and snippets.

@emmaodia
Last active January 24, 2020 02:14
Show Gist options
  • Save emmaodia/185a3397ba52fc04bc36839428107844 to your computer and use it in GitHub Desktop.
Save emmaodia/185a3397ba52fc04bc36839428107844 to your computer and use it in GitHub Desktop.
// Configure the Facebook strategy for use by Passport.
passport.use(new Strategy({ //This is class constructor argument telling Passport to create a new Facebook Auth Strategy
clientID: process.env['FACEBOOK_CLIENT_ID'],//The App ID generated when app was created on https://developers.facebook.com/
clientSecret: process.env['FACEBOOK_CLIENT_SECRET'],//The App Secret generated when app was created on https://developers.facebook.com/
callbackURL: 'http://localhost:3000/api/v1/user/return',
profile: ['id', 'displayName'] // You have the option to specify the profile objects you want returned
},
function(accessToken, refreshToken, profile, done) {
//Check the DB to find a User with the profile.id
User.findOne({ facebook_id: profile.id }, function(err, user) {//See if a User already exists with the Facebook ID
if(err) {
console.log(err); // handle errors!
}
if (user) {
done(null, user); //If User already exists login as stated on line 10 return User
} else { //else create a new User
user = new User({
facebook_id: profile.id, //pass in the id and displayName params from Facebook
name: profile.displayName
});
user.save(function(err) { //Save User if there are no errors else redirect to login route
if(err) {
console.log(err); // handle errors!
} else {
console.log("saving user ...");
done(null, user);
}
});
}
});
}
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment