Search code examples
feathersjs

How to authenticate with a Facebook token?


I'm POST'ing to http://localhost:3030/authentication and trying to retrieve a JWT and create a user model using Facebook oAuth. My client side is React and seperate to the API.

My body:

{
    "strategy": "facebook",
    "accessToken": "EAALOM2rXQ50BAL5vERqa6YTJzyKZAeG4Yd9ZBPuqHfRmFzOpsQiTHEc1uS1sBaot7V9F7JlufpAWqqJoHIG8RCghLdBIKssKRyQqMLxiHvCPTjVDXqaR1rM4FhEXk55nFU0ZBZBP2KNVOoDNAGUBtJAOKaUdbeszLE0gXxgoF0DWgZBsRfVsZBFRBenQLMZBwZCDPIF606aHrA0CdDrcTRqSbcJWZCSlYIMwZD",
    "facebookId": "10155772119707013",
    "email": "joshn@hotmail.com.au"
}

But I'm getting a HTML page response saying 'You are not logged in: You are not logged in. Please log in and try again.'

Same issue @ https://github.com/feathersjs/authentication-oauth2/issues/22 if anyone has figured out a solution to this.


Solution

  • This is possible with https://github.com/drudge/passport-facebook-token

    My solution (inside authentication.js) is to extend a custom Passport strategy:

    app.passport.use('facebook-token', new FacebookTokenStrategy({
            clientID: process.env.FACEBOOK_CLIENT_ID,
            clientSecret: process.env.FACEBOOK_CLIENT_SECRET
        }, function (accessToken, refreshToken, profile, done) {
            app.service('users').find({
                query: {
                    facebookId: profile.id
                }
            }).then((userData) => {
                if (userData.data.length > 0) {
                    return done(null, userData.data[0]);
                } else {
                    app.service('users').create({}).then((data) => {
                        return done(null, data);
                    });
                }
            });
        }
    ));