onLoginFinished callback is not fired in Login with Facebook

Viewed 773

I'm building a simple React Native app which implements "Login with Facebook" using react-native-fbsdk I can login and logout as the button changes from Log in to Log out when logging with credentials, but the onLoginFinished callback never gets fired, though onLogoutFinished works properly.

Here is my code:

import React, { Component } from 'react';
import { View,Alert,Button,Text } from 'react-native';
import { LoginButton, AccessToken } from 'react-native-fbsdk';

export default class Login extends Component {
  constructor(props) {
    super(props);
    this.state = {
      userInfo: null
    }
  }

  onFacebookLoginFinished = (error, result) => {
    if (error) {
      Alert.alert("login has error: " + result.error);
    } else if (result.isCancelled) {
      Alert.alert("login is cancelled.");
    } else {
      AccessToken.getCurrentAccessToken().then(
        (data) => {
          Alert.alert(data.accessToken.toString())
          this.props.navigation.navigate('Home')
        }
      )
    }
  }

  render() {
    return (
      <View>
        <LoginButton
          onLoginFinished={this.onFacebookLoginFinished}
          onLogoutFinished={() => Alert.alert("logout.")}/>
      </View>
    );
  }
};
4 Answers

I think you should try this.

onLoginFinished={(error, result) => this.onFacebookLoginFinished(error, result)}

Change the function into this.

onFacebookLoginFinished (error, result){
    ...
}

Change the onLoginFinished method from:

onLoginFinished={this.onFacebookLoginFinished}

To:

onLoginFinished={(error, result) => this.onFacebookLoginFinished(error, result)}

Hope this Helps!

Could you try this code?

Try doing it yourself in a function.

onLoginFinished={(error, data) => {
               if (error) {
      Alert.alert("login has error: " + result.error);
    } else if (result.isCancelled) {
      Alert.alert("login is cancelled.");
    } else {
      AccessToken.getCurrentAccessToken().then(
        (data) => {
          Alert.alert(data.accessToken.toString())
          this.props.navigation.navigate('Home')
        }
      )
    }
          }}

You're accessing the component instance (this), so you'll need to bind to your onLoginFinished function, perefarbly in your constructor, i.e.

constructor(props) {
  super(props);
  this.state = {
    userInfo: null
  }

  this.onFacebookLoginFinished = this.onFacebookLoginFinished.bind(this);
}

See further explanation and more options here: https://reactjs.org/docs/faq-functions.html#bind-in-constructor-es2015

Related