react-native-oauth icon indicating copy to clipboard operation
react-native-oauth copied to clipboard

Unable to log in via Facebook on Android

Open maxcodes opened this issue 7 years ago • 4 comments

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 avatar Oct 12 '18 23:10 maxcodes

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 avatar Oct 15 '18 18:10 maxcodes

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) {

maxcodes avatar Oct 15 '18 23:10 maxcodes

@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

francoro avatar Mar 18 '19 18:03 francoro

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

maxcodes avatar Apr 16 '19 20:04 maxcodes