passport-facebook-extension

0.0.7 • Public • Published

passport-facebook-extension

Coverage Dependencies

I created this module, because after setting up Passport strategy for authenticating with Facebook and their recent v2.3 API (Passport-facebook) i couldnt know what permissions was given by the user and if the user accepted the user-friends permission, i decided to make this module and share it for whoever needs it.

Install

$ npm install passport-facebook-extension

Usage

Require the module

var PassportFacebookExtension = require('passport-facebook-extension');

Initialize and use the FBExtension module

To use these module we asume that you integrated Passport and Passport-Facebook with Expresss, so this code should look familiar to you.

passport.use(new FacebookStrategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET,
    callbackURL: "/auth/facebook/callback"
  },
  function(accessToken, refreshToken, profile, done) {
    var FBExtension = new PassportFacebookExtension(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET);
    // After initialize the module you can check the given permission by:
    FBExtension.permissionsGiven(profile.id, accessToken)
        .then(function (permissions) {
            profile.permissions = permissions;
            done(null, profile);
        }).fail(function (e) {
            console.log(e);
        });
  }
));

Methods

permissionsGiven (facebookid, accessToken)

  • facebookid - Facebook id from the user that just logged.
  • accessToken - Access Token generated by Facebook and ported to you by Passport-Facebook after accepted the app.
    FBExtension.permissionsGiven(profile.id, accessToken)
        .then(function (permissions) {
            profile.permissions = permissions;
            done(null, profile);
        }).fail(function (e) {
            console.log(e);
        });

If no error was triggered then will be executed with a callback and an array (permissions) passed as parameter:

    [
        {
        "permission":"user_friends",
        "status":"granted"},
        {"permission":"read_stream","status":"granted"},
        {"permission":"public_profile","status":"granted"}
    ]

friendsUsingApp (facebookid, accessToken)

  • facebookid - Facebook id from the user that just logged.
  • accessToken - Access Token generated by Facebook and ported to you by Passport-Facebook after accepted the app.
    FBExtension.friendsUsingApp(profile.id, accessToken)
        .then(function (friends) {
            profile.friends = friends;
            done(null, profile);
        }).fail(function (e) {
            console.log(e);
        });

If no error was triggered then will be executed with a callback and an array (friends) passed as parameter:

   [{"name":"Erick Arroyo","id":"87854546852123"}]

extendShortToken (accessToken)

  • accessToken - Access Token generated by Facebook and ported to you by Passport-Facebook after accepted the app.
    FBExtension.extendShortToken(accessToken)
        .then(function(response){
            console.log('Long-lived Token: ',response.access_token); // CAAWhckuwzlUBAIIZAZACh2uVpaRXv4NTgdZdRNiM2enIZBhRe5ur.....
            console.log('Expires in : ',response.expires+' secs.');  // 60 days in seconds
        })
        .fail(function(error){
            console.log(error)
        });

If no error was triggered then will be executed with a callback and an array (friends) passed as parameter:

   {
    "access_token": "CAAWhckuwzlUBAIIZAZACh2uVpaRXv4NTgdZdRNiM2enIZBhRe5ur....",
    "expires": "5184000"
   }
  • access_token - Long-lived token generated.
  • expires - Live in seconds of the long-lived token generated, ussually 60 days.

Demo

Issues

Facebook's OAuth 2.0 implementation has a bug in which the fragment #_=_ is appended to the callback URL. This appears to affect Firefox and Chrome, but not Safari. This fragment can be removed via client-side JavaScript, and @niftylettuce provides a suggested workaround. Developers are encouraged to direct their complaints to Facebook in an effort to get them to implement a proper fix for this issue.

Credits

License

The MIT License

Copyright (c) 2015 Erick Arroyo <http://erickarroyo.com/>

Readme

Keywords

none

Package Sidebar

Install

npm i passport-facebook-extension

Weekly Downloads

5

Version

0.0.7

License

none

Last publish

Collaborators

  • coerick