drudge / passport-facebook-token

Passport strategy for authenticating with Facebook access tokens using the OAuth 2.0 API.
MIT License
390 stars 80 forks source link

Emails are empty #91

Open Nainabbas opened 4 years ago

Nainabbas commented 4 years ago

Even though I have the access_token with email access and i have write the scope of email in passport it's not getting the email of the user.

Nainabbas commented 4 years ago

` passport.use( "facebookToken", new FacebookTokenStrategy( { clientID: process.env.FB_APP_ID, clientSecret: process.env.FB_APP_SECRET, passReqToCallback: true, profileFields: ["id", "displayName", "name", "gender", "emails"] }, async (req, accessToken, refreshToken, profile, done) => { try { //check if user exsists in database //... //if not make new user //.... //genrate token console.log(profile); var token = await jwt.sign(profile, process.env.JWT_SECRET); req.access_token = token; return done(null, req.access_token); } catch (err) { done(err, false, err.message); } } ) );

`

Nainabbas commented 4 years ago

router.post( "/login/facebook", passport.authenticate("facebookToken", { scope: ["email", "public_profile"], session: false }), authController.loginFacebook );

ghaiklor commented 4 years ago

Did you try use emails instead of email?

Nainabbas commented 4 years ago

not worked

Shyam-Chen commented 3 years ago

Client Requests:

FB.login(
  function(response) {
    if (response.status === 'connected') {
      axios
        .get('http://0.0.0.0:3000/auth/facebook/token', {
          params: {
            access_token: response.authResponse.accessToken,
          },
        })
        .then(({ data }) => {
          console.log('data =', data);
        });
    } else {
      // The person is not logged into your webpage or we are unable to tell.
    }
  },
  { scope: 'email' },
);

Use in Vue:

<template>
  <FacebookLogin app-id="XXX" version="v9.0" @login="fbLogin" />
</template>

<script>
import FacebookLogin from 'vue-facebook-login-component';
import axios from 'axios';

export default {
  components: {
    FacebookLogin,
  },
  methods: {
    fbLogin({ authResponse }) {
      axios
        .get('http://0.0.0.0:3000/auth/facebook/token', {
          params: {
            access_token: authResponse.accessToken,
          },
        })
        .then(({ data }) => {
          console.log('data =', data);
        });
    },
  },
};
</script>
MakakWasTaken commented 3 years ago

I recently started using this framework as well and experienced the same issue as you are having with the email not being present. I was using a Facebook test user and got the access token through the Facebook API dashboard.

The first thing I noted with your code is that the router should use passport.authenticate("passport-facebook-token") instead. Like this:

router.post(
  "/login/facebook",
  passport.authenticate("passport-facebook-token", {
    scope: ["email", "public_profile"],
    session: false,
  }),
  authController.loginFacebook
);

I am not sure this fixes anything for you.

My next problem was that my test user did not have the email permission for some reason. You can check the permissions by sending a request to the following URL. (With your access token as your Authorization parameter): https://graph.facebook.com/v10.0/me/permissions Mine said something like:

[...]
        {
            "permission": "email",
            "status": "expired"
        }
[...]

What fixed it for me was using my own user instead. (I still have not figured out why this is happening to the test user). You can get a personal access_token from: https://developers.facebook.com/tools/explorer/

Hope this helps