facebookarchive / react-native-fbsdk

A React Native wrapper around the Facebook SDKs for Android and iOS. Provides access to Facebook login, sharing, graph requests, app events etc.
https://developers.facebook.com/docs/react-native
Other
2.99k stars 908 forks source link

Add method to listen for changes to the access token #666

Closed matt-oakes closed 4 years ago

matt-oakes commented 4 years ago

This PR adds a method which allows you to monitor the access token for changes. This enable you to have a hook which accurately represents the Facebook Access Token which will update when the user logs in or out.

It is implemented using the notifications (iOS) and tracker (Android) which the natvie Facebook SDKs provide.

Test Plan:

Tested on both platforms using the following hook code:

const useFacebookToken = () => {
  const [accessToken, setAccessToken] = React.useState<FBSDK.AccessToken>();

  React.useEffect(() => {
    FBSDK.AccessToken.getCurrentAccessToken().then(setAccessToken);

    return FBSDK.AccessToken.addListener(setAccessToken);
  }, []);

  return accessToken;
};
matt-oakes commented 4 years ago

For anyone who wants to install and try this now, you can install it using this:

yarn add react-native-fbsdk@https://github.com/matt-oakes/react-native-fbsdk.git#access-token-listener

or:

npm install --save react-native-fbsdk@https://github.com/matt-oakes/react-native-fbsdk.git#access-token-listener

ps. Loving the PR number I ended up with! 😈