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 906 forks source link

Getting null when calling AccessToken.getCurrentAccessToken() after restarting the app #727

Open ManalLiaquat opened 4 years ago

ManalLiaquat commented 4 years ago

🐛 Bug Report

I want accessToken in componentDidMount but getting null in the promise response when I restart (not reload) the app. I think that FBSDK login is not persisting the accessToken in the app. This only happens in iOS (Both: real device and simulator) and in Android, it works fine.

To Reproduce

  1. Login into the app
  2. Calling AccessToken.getCurrentAccessToken() after login success (Note: I'm getting accessToken two times)
    • after login (it works well)
    • in the componentDidMount (I'm getting null even after login)
  3. After Login, I close & reopen (not reloading: in reloading the app, I'm getting the accessToken) the app (to invoke componentDidMount) but getting null in promise response.

Expected Behavior

As I said in the bug report section, that I'm going to get an access token in componentDidMount but getting null in the promise response.

Code Example

  const _checkFbAccessToken = async () => {
    try {
      const { accessToken } = await AccessToken.getCurrentAccessToken();
      console.log(accessToken, ' ****accessToken');
    } catch (error) {
      // error in getting accessToken 
    }
  };

Environment

System:
    OS: macOS 10.15.1
    CPU: (8) x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
    Memory: 11.27 GB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 11.12.0 - /usr/local/bin/node
    Yarn: 1.15.2 - /usr/local/bin/yarn
    npm: 6.7.0 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK:
      API Levels: 23, 25, 26, 27, 28
      Build Tools: 28.0.2, 28.0.3
      System Images: android-28 | Google APIs Intel x86 Atom
  IDEs:
    Android Studio: 3.3 AI-182.5107.16.33.5314842
    Xcode: 11.3.1/11C504 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.8.6 => 16.8.6 
    react-native: 0.60.5 => 0.60.5 
  npmGlobalPackages:
    react-native-cli: 2.0.1
muhammadzumair commented 4 years ago

I am also facing this issue in IOS, but in Android, it works perfectly and return accessToken every time I open the app.

AdeenUlHaq3 commented 4 years ago

I'm also facing this issue in iOS.

pix2D commented 4 years ago

Same issue here. Only way to "fix" it is to kill the app and open it again.

eddysung88 commented 4 years ago

I got same issue, but i solved adding some code in AppDelegate.m .

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#ifdef FB_SONARKIT_ENABLED
  InitializeFlipper(application);
#endif

  // add this
  [[FBSDKApplicationDelegate sharedInstance] application:application
  didFinishLaunchingWithOptions:launchOptions];
  // add this

  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"myReactnativeExample"
                                            initialProperties:nil];

  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];

  return YES;
}