passport-facebook-token icon indicating copy to clipboard operation
passport-facebook-token copied to clipboard

Emails are empty

Open Nainabbas opened this issue 5 years ago • 6 comments

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 avatar Jan 15 '20 06:01 Nainabbas

` 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 avatar Jan 15 '20 06:01 Nainabbas

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

Nainabbas avatar Jan 15 '20 06:01 Nainabbas

Did you try use emails instead of email?

ghaiklor avatar Jan 15 '20 11:01 ghaiklor

not worked

Nainabbas avatar Jan 23 '20 08:01 Nainabbas

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>

Shyam-Chen avatar Jan 06 '21 01:01 Shyam-Chen

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

MakakWasTaken avatar May 04 '21 11:05 MakakWasTaken