Search code examples
facebookfacebook-graph-apireact-nativefbsdkreact-native-fbsdk

React-native fbsdk get name and email


I'm working in with react-native-fbsdk. I want to get username and email to show up to the user when they're registered to the app in order to create a profile for the app, but no data is shown. How can I get these data and how can I show it in a different screen.

This is my code

import FBSDK from 'react-native-fbsdk'
import { LoginButton,AccessToken,GraphRequest,GraphRequestManager} from 'react-native-fbsdk';


const {
  LoginManager,
} = FBSDK;

<LoginButton
        readPermissions={['public_profile']}
          onLoginFinished={
            (error, result) => {
              if (error) {
                alert("login has error: " + result.error);
              } else if (result.isCancelled) {
                alert("login is cancelled.");
              } else {
                AccessToken.getCurrentAccessToken().then(
                  (data) => {
                    const infoRequest = new GraphRequest(
                      '/me?fields=name,picture',
                      null,
                      this._responseInfoCallback
                    );
                    // Start the graph request.
                    new GraphRequestManager().addRequest(infoRequest).start();
                  }
                )
              }
            }
          }
          onLogoutFinished={() => alert("logout.")}/>
      </View>
    );
  }
  _responseInfoCallback = (error, result) => {
    if (error) {
      alert('Error fetching data: ' + error.toString());
    } else {
      alert('Result Name: ' + result.name);
    }
  }

}

Thanks friends


Solution

  • I retrieve facebook login while I using firebase.

    1. import this file:

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

    2. Firebase login code work when I click button:

      export const fbLogin = () => {

      LoginManager.logInWithReadPermissions(["public_profile", "email"])
        .then(result => {
          if (result.isCancelled) {
            console.log("Login was cancelled");
          }
          return AccessToken.getCurrentAccessToken();
        })
        .then(data => {
          const credential = firebase.auth.FacebookAuthProvider.credential(
            data.accessToken
          );
         firebase
          .auth()
          .signInWithCredential(credential)
          .then(result => {
           Toast.show({
            text: "Sucessfully",
            position: "top"
          });
         console.log("Successfully Login", result);
       })
        .catch(error => {
         console.log("Failed", error);
           });
        })
        .catch(err => {
          console.log("fail", err);
        }); 
      

      };

    I retrive my facebook login data

     console.log("Successfully Login", result);
    

    result contains username, email, picture and ..ect credentials