Search code examples
facebookreact-nativefbsdkreact-native-fbsdk

React Native Facebook SDK ShareDialog asks to login again


I'm currently doing a project using react native where I need to let users to login using facebook. So I use React-native-fbsdk LoginButton component to let users to login and get the access token. Also part of the project needs let users to share links to their profiles.

In order to do that, I followed as GitHub repo of Facebook which says how to add ShareDialog. After adding both LoginButton and ShareDialog, I ran the app. Login was successful and I got the access token. But when I try to share a link, it asks me to login again. If I login with ShareDialog, it let me to share post and everything as expected.

But if I logout with LoginButton after login with ShareDialog too, both components perform logout successfully. Problem is Login does not handle for both components with single login.

Below is my code.

render() {
return (
  <View style={styles.container}>
    <LoginButton
      publishPermissions={["publish_actions"]}
      onLoginFinished={
        (error, result) => {
          if (error) {
            alert("Login failed with error: " + error.message);
          } else if (result.isCancelled) {
            alert("Login was cancelled");
          } else {
            alert("Login was successful with permissions: " + result.grantedPermissions)
          }
        }
      }
      onLogoutFinished={() => alert("User logged out")}/>
    <TouchableHighlight onPress={this.shareLinkWithShareDialog}>
      <Text style={styles.shareText}>Share link with ShareDialog</Text>
    </TouchableHighlight>
  </View>
 );
}

shareLinkWithShareDialog = () => {

const shareLinkContent = {
  contentType: 'link',
  contentUrl: 'https://www.sample.com/',
  contentDescription: 'Test Sharing Description'
};
 var tmp = this;
 ShareDialog.canShow(shareLinkContent).then(
 (canShow) => {
    if (canShow) {
      return ShareDialog.show(shareLinkContent);
    }
  }
).then(
 (result) => {
    if (result.isCancelled) {
      alert('Share cancelled');
    } else {
      alert('Share success with postId: ' + result.postId);
    }
  },
 (error) => {
    alert('Share fail with error: ' + error);
  }
 );
}

Please help me here. I cannot identify what's wrong with this.

Note

I checked this using a Test User of Facebook and it also has all relevant permissions including manage_pages and test user has it's own Test Page too.


Solution

  • I got a similar problem and that was because I hadn't installed facebook app on my phone. Seems like your code is fine.

    However I haven't used Test Page and Test User options in facebook but I hope they are no different than a normal user when it comes to functionalities. Just try to install the facebook app and run your app. Should be fine.