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

Unable to log in via Facebook on Android #231

Open maxcodes opened 6 years ago

maxcodes commented 6 years ago

I'm having the exact same issue as #202. Copy-pasta:

This is only on Android. iOS works fine.

It seems the issue is related to the default callback: http://localhost/facebook. Facebook doesn't allow http callbacks anymore, so we're kind of stuck here. I tried all possible combinations of settings, even tried https://localhost/facebook just for the funz, but that didn't work either.

I'm investigating and can probably send a PR fix, but any pointers would be very helpful. Thanks!

maxcodes commented 6 years ago

So, I managed to fix this by changing one line in the OAuthManagerModule.java. Instead of using http://localhost/facebook as a callback, I changed it to https://localhost/facebook both in the Facebook Valid OAuth Redirect URIs and here:

  @ReactMethod
  public void authorize(
    final String providerName, 
    @Nullable final ReadableMap params, 
    final Callback callback) 
  {
    try {
      final OAuthManagerModule self = this;
      final HashMap<String,Object> cfg = this.getConfiguration(providerName);
      final String authVersion = (String) cfg.get("auth_version");
      Activity activity = this.getCurrentActivity();
      FragmentManager fragmentManager = activity.getFragmentManager();
      String callbackUrl = "http://localhost/" + providerName; # <--- change this line to https

      ...

Happy to send a PR if this is the recommended way of fixing it 🙂

maxcodes commented 6 years ago

So, the above snippet works if you're only working with facebook, but obviously breaks the other providers. The final snippet looks like this:

patch-package
--- a/node_modules/react-native-oauth/android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java
+++ b/node_modules/react-native-oauth/android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java
@@ -114,7 +114,12 @@ class OAuthManagerModule extends ReactContextBaseJavaModule {
       final String authVersion = (String) cfg.get("auth_version");
       Activity activity = this.getCurrentActivity();
       FragmentManager fragmentManager = activity.getFragmentManager();
-      String callbackUrl = "http://localhost/" + providerName;
+      String callbackUrl;
+      if (providerName.equals("facebook")) {
+        callbackUrl = "https://localhost/" + providerName;
+      } else {
+        callbackUrl = "http://localhost/" + providerName;
+      }

       OAuthManagerOnAccessTokenListener listener = new OAuthManagerOnAccessTokenListener() {
         public void onRequestTokenError(final Exception ex) {
francoro commented 5 years ago

@maxcodes Hi max, I have question. This code you put above work also when the user is using LTE not wifi? My question is because use localhost , is okay that for LTE ?

Thanks

maxcodes commented 5 years ago

Hey @francoro, I don't remember that being a problem, since localhost always refers to the local device. Cheers!