kcuhcx2
kcuhcx2

Reputation: 353

React-native Log in with Facebook App refreshes app

I am using react-native-fbsdk and am on IOS. When users log in with Facebook their are two buttons they can press. Log in with the Facebook App or Log in with Phone Number or Email Address. Logging in with the Phone Number or email address works perfectly, I get the user object and create a user account for them. But when users press Log in with the Facebook App I don't get sent a user object, and the whole app refreshes where they get pushed to the log in page and the Facebook button has turned to Log out. I am not too sure on how to approach this as when users log in with the Facebook App none of my console.logs or alerts are getting called and the whole app just refreshes. Any help would be very much appreciated.

           <LoginButton
            readPermissions={["email","public_profile"]}
            onLoginFinished={
              (error, result) => {
                console.log("onLoginFinished: ", result);
                if (error) {
                  console.log("login has error: ", result.error);
                } else if (result.isCancelled) {
                  console.log("login is cancelled.");
                } else {
                  AccessToken.getCurrentAccessToken().then(
                    (data) => {
                      let accessToken = data.accessToken
                      console.log("accessToken.toString(): ", accessToken.toString())

                      const responseInfoCallback = (error, result) => {
                        if (error) {
                          console.log('Error fetching data: ', error)
                          //lert('Error fetching data: ' + error.toString());
                        } else {
                          console.log("responseInfoCallback:", result)
                          this.state.fbobj = result;
                          this.sendAjax();
                          //alert('Success fetching data: ' + result.toString());
                        }
                      }

                      const infoRequest = new GraphRequest(
                        '/me',
                        {
                          accessToken: accessToken,
                          parameters: {
                            fields: {
                              string: 'email,name'
                            }
                          }
                        },
                        responseInfoCallback
                      );

                      // Start the graph request.
                      new GraphRequestManager().addRequest(infoRequest).start()

                    }
                  )
                }
              }
            }
            onLogoutFinished={() => console.log("Logout")}

Upvotes: 0

Views: 183

Answers (1)

Lucas Z.
Lucas Z.

Reputation: 435

In your button action, you will need something like this:

import { LoginManager, AccessToken } from 'react-native-fbsdk'

handlePressFacebookLogin = () => {
    LoginManager.logInWithReadPermissions(['public_profile', 'email']).then(
      (result) => {
        if (!result.isCancelled) {
          AccessToken.getCurrentAccessToken().then((data) => {
            console.log(data)
          })
        }
      }
    )
  }

Upvotes: 1

Related Questions