Giter VIP home page Giter VIP logo

Comments (11)

ekryski avatar ekryski commented on July 1, 2024 2

Yes @marshallswain has it almost correct. It needs to be whatever the passport strategy requires for params. So in this case:

{
  strategy: 'facebook-token',
  access_token: 'your facebook access token'
}

from authentication-oauth2.

marshallswain avatar marshallswain commented on July 1, 2024 1

We definitely need a guide that shows how to do this. I know you need. To use the passport-Facebook-token strategy. I'm not sure what else is needed.

from authentication-oauth2.

ysfzrn avatar ysfzrn commented on July 1, 2024 1

No, I didn't. After trying, I will write about it

from authentication-oauth2.

daffl avatar daffl commented on July 1, 2024 1

So @00Freezy00 wrote up a great tutorial about this in https://medium.com/@jackzhang0096/how-to-setup-oauth-2-0-token-strategy-on-feathersjs-1d77cc32118b

With that I think we can close this.

from authentication-oauth2.

ysfzrn avatar ysfzrn commented on July 1, 2024

Same issue

from authentication-oauth2.

marshallswain avatar marshallswain commented on July 1, 2024

@imdpk @ysfzrn did either of you try my suggestion?

from authentication-oauth2.

marshallswain avatar marshallswain commented on July 1, 2024

You'll need to use this strategy: https://github.com/drudge/passport-facebook-token.

from authentication-oauth2.

marshallswain avatar marshallswain commented on July 1, 2024

I think you actually use the authentication endpoint with a payload something like this:

{
  strategy: 'facebook-token', // or whatever you call it with the `name` options
  accessToken: 'the token'
}

Then you should get back a JWT accessToken for your Feathers API.

from authentication-oauth2.

ysfzrn avatar ysfzrn commented on July 1, 2024

On the server side

1-After adding facebook authentication with feathers-cli, you have to edit config files with your facebook client and your facebook secret

2-In authentication.js, check authentication name,

...
 app.configure(
    oauth2(
      Object.assign(
        {
          name: "facebook",
          Strategy: FacebookStrategy,
        },
        config.facebook
      )
    )
  );
...

3- If you want to pass more info with your payload, You have to customize JWT payload

  app.service("authentication").hooks({
    before: {
      create: [
        authentication.hooks.authenticate(config.strategies),
        customizeJWTPayload()
      ],
      remove: [authentication.hooks.authenticate("jwt")]
    }
  });
};

function customizeJWTPayload() {
  return function(hook) {
    hook.params.payload = {
      facebookId: hook.data.facebookId  //facebookId is parameter from client side. We will send
    };
    return Promise.resolve(hook);
  };
}

On the client side

I have to say, I'm using redux saga and I'm calling my functions calling one by one. Of course some of them are async functions and return Promise.

1-Call the LoginManager.logInWithReadPermissions for getting permissions

import { LoginManager} from "react-native-fbsdk";

export function loginWithFacebook(app) {
  return new Promise((resolve, reject) => {
    LoginManager.logInWithReadPermissions(["public_profile", "email"])
      .then(FBloginResult => {
        if (FBloginResult.isCancelled) {
          throw new Error("Login cancelled");
        }

        if (FBloginResult.deniedPermissions) {
          throw new Error("We need the requested permissions");
        }
      })
      .then(result => {
        resolve(result);
      })
      .catch(error => {
        reject(error);
      });
  });
}

2- After permission accept, get user info from GraphRequestManager.

import {LoginManager,GraphRequest,GraphRequestManager} from "react-native-fbsdk";

const facebookParams = "id,name,email,picture.width(600).height(800)";

export function getUserFacebookInfo(app) {
  return new Promise((resolve, reject) => {
    const profileInfoCallback = (error, profileInfo) => {
      if (error) reject(error);
      
      resolve(profileInfo);
    };

    const profileInfoRequest = new GraphRequest(
      "/me",
      {
        parameters: {
          fields: {
            string: facebookParams
          }
        }
      },
      profileInfoCallback
    );

    new GraphRequestManager().addRequest(profileInfoRequest).start();
  });
}

3- Ok, now we have permission and user info. Let's authenticate our app

import {LoginManager,GraphRequest,GraphRequestManager,AccessToken} from "react-native-fbsdk";

export function authenticateWithFacebook(app, promiseSuccess, userInfo) {
  return app
    .authenticate({
      strategy: "facebook",  // this name must be same with server side name.
      accessToken: AccessToken,
      facebookId: userInfo.id,  // we can pass any parameter what we want for customizing payload
      email: userInfo.email,     
      name: userInfo.name,
      image: userInfo.picture.data.url
    })
    .then(response => {
      app.set("token", response.accessToken);
      return app.passport.verifyJWT(response.accessToken);
    })
    .then(payload => {
      return app
        .service("users")
        .find({ query: { facebookId: payload.facebookId } });  //remember we have added this payload on server side
    })
    .then(user => {
      app.set("user", user);
      return app.get("token");
    });
}

Actually this way, I couldn't authenticate my app . I have doubt about last part. Saving user doesn't seems good. I tried create customVerifier like this but I couldn't.

from authentication-oauth2.

oun avatar oun commented on July 1, 2024

Hi @ysfzrn. I follow your guide but it does not create/update user after authenticate with facebook. Can you please provide an example code?

Updated: I changed to passport-facebook-token and it's working now. Thanks @marshallswain @ekryski

from authentication-oauth2.

gijun19 avatar gijun19 commented on July 1, 2024

Any updates on this?

from authentication-oauth2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.