fullstackreact / react-native-oauth

A react-native wrapper for social authentication login for both Android and iOS
https://fullstackreact.com
MIT License
801 stars 214 forks source link

Twitter Authorize: Stuck on "Redirecting you back to the application..." #146

Open joncursi opened 7 years ago

joncursi commented 7 years ago

After authorizing with Twitter, I get stranded on the following screen:

screen shot 2017-08-20 at 8 52 21 pm

Because I'm not automatically navigated back to the app, if you press the "Done" button, you will return to the app but the access tokens do not get stored.

I have ensured that I have a URL Type defined for Twitter and that a callback URL is set in my Twitter app settings. Any other ideas on why the redirect is failing silently?

joncursi commented 7 years ago

I'm debugging in XCode and I see that valid params are being sent to twitter on the initial authorize call:

2017-08-20 21:20:23.809 redbirdNative[16785:198848] Calling authorizeWithUrl: twitter with callbackURL: cheddur://oauth-response/twitter
 {
    "access_token_url" = "https://api.twitter.com/oauth/access_token";
    "api_url" = "https://api.twitter.com";
    "app_name" = cheddur;
    "auth_version" = "1.0";
    "authorize_url" = "https://api.twitter.com/oauth/authorize";
    "callback_url" = "cheddur://oauth-response/twitter";
    "consumer_key" = xxxx;
    "consumer_secret" = xxxx;
    "request_token_url" = "https://api.twitter.com/oauth/request_token";
    scopes = email;
}

But when the user signs in and clicks "Authorize", nothing else logs out and the screen doesn't redirect. Silent failure.

joncursi commented 7 years ago

If I open up safari and manually type in the callback url:

cheddur://oauth-response/twitter

Safari will prompt me to open the app, and it will indeed open the app and redirect. I get this error, which makes sense, because I'm going to the redirect URL from Safari without passing any params:

screen shot 2017-08-20 at 9 49 29 pm

So I am at a loss on why Twitter isn't redirecting as expected.

joncursi commented 7 years ago

Alright so I figured out the issue. The code block that we put into AppDelegate.m for this library conflicts with the code block also given by react-native-fbsdk.

FBSDK:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

  BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
    openURL:url
    sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
    annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
  ];
  return handled;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
  [FBSDKAppEvents activateApp];
}

OAUTH:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  return [OAuthManager handleOpenUrl:application
                             openURL:url
                   sourceApplication:sourceApplication
                          annotation:annotation];
}

My solution = merge the two into a custom method:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
  if ([url.absoluteString containsString:@"twitter"]){
    return [OAuthManager handleOpenUrl:application
                               openURL:url
                     sourceApplication:@"twitter"
                            annotation:nil];
  } else{
    BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
      openURL:url
      sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
      annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
    ];
    return handled;
  }
}

I only have a rough idea of what I just wrote, since I do not know Objective C, but it works. I am definitely open to suggestions on how to improve this from those who actually understand this.

Thanks!

hotelcally commented 4 years ago

I’ve had this problem for two weeks. I’ll keep digging and asking and submitting until it’s fixed.

rowlandholden76 commented 4 years ago

I had this issue with chrome. I switched to Samsung Internet browser and it went through with no issues. Looks like a potential browser issue?

Mirant123 commented 4 years ago

From ios13 we have scenedelegate file so for that with appdelegate openURL method we also need to manage the method in scenedelegate file via the below delegate method for twitter.

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { if let openURLContext = URLContexts.first{ let url = openURLContext.url let options: [AnyHashable : Any] = [ UIApplication.OpenURLOptionsKey.annotation : openURLContext.options.annotation as Any, UIApplication.OpenURLOptionsKey.sourceApplication : openURLContext.options.sourceApplication as Any, UIApplication.OpenURLOptionsKey.openInPlace : openURLContext.options.openInPlace ] TWTRTwitter.sharedInstance().application(UIApplication.shared, open: url, options: options) } }