Skip to content

Changed to store param values in shared preferences #1

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jan 19, 2022

Conversation

Nirodha26
Copy link
Owner

Fixes #672, #360

Description

Fixes app crash issue on authorize for Android

Steps to verify

Enable "Don't Keep Activities" setting in your android Device's Developer Setting,
Authenticate user using authorize method

@asami95
Copy link

asami95 commented Aug 29, 2022

Hello @Nirodha26,
Thank you so much for your work, I really appreciate your efforts!

I tried to install your version the crash was solved, but It produces a new crash:
com.rnappauth.RNAppAuthModule.onActivityResult java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference

Is there any way to solve this crash also?
Thanks in advance!

@yberstad
Copy link

Hi @asami95!

I'm experinecing the same issue, did you find any fix / workaround?

Thanks in advance!

BR, Øyvind.

@yberstad
Copy link

Hi!

I have made a pathc file to avoid the error above (added null check on skipCodeExchange and usePKCE. I also added a try/catch around the whole onActivityResult. I have not seen the error after this patch.

I'm currently using

    "react-native": "0.69.3",
    "react-native-app-auth": "7.0.0-rc2",
diff --git a/node_modules/react-native-app-auth/android/src/main/java/com/rnappauth/RNAppAuthModule.java b/node_modules/react-native-app-auth/android/src/main/java/com/rnappauth/RNAppAuthModule.java
index b775886..614d068 100644
--- a/node_modules/react-native-app-auth/android/src/main/java/com/rnappauth/RNAppAuthModule.java
+++ b/node_modules/react-native-app-auth/android/src/main/java/com/rnappauth/RNAppAuthModule.java
@@ -479,95 +479,104 @@ public class RNAppAuthModule extends ReactContextBaseJavaModule implements Activ
      */
     @Override
     public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
-        if (requestCode == 52) {
-            if (data == null) {
-                if (promise != null) {
-                    promise.reject("authentication_error", "Data intent is null" );
+        try {
+            if (requestCode == 52) {
+                if (data == null) {
+                    if (promise != null) {
+                        promise.reject("authentication_error", "Data intent is null" );
+                    }
+                    return;
                 }
-                return;
-            }

-            final AuthorizationResponse response = AuthorizationResponse.fromIntent(data);
-            AuthorizationException ex = AuthorizationException.fromIntent(data);
-            if (ex != null) {
-                if (promise != null) {
-                    handleAuthorizationException("authentication_error", ex, promise);
+                final AuthorizationResponse response = AuthorizationResponse.fromIntent(data);
+                AuthorizationException ex = AuthorizationException.fromIntent(data);
+                if (ex != null) {
+                    if (promise != null) {
+                        handleAuthorizationException("authentication_error", ex, promise);
+                    }
+                    return;
                 }
-                return;
-            }

-            if (this.skipCodeExchange) {
-                WritableMap map;
-                if (this.usePKCE && this.codeVerifier != null) {
-                    map = TokenResponseFactory.authorizationCodeResponseToMap(response, this.codeVerifier);
-                } else {
-                    map = TokenResponseFactory.authorizationResponseToMap(response);
-                }
+                if (this.skipCodeExchange != null && this.skipCodeExchange) {
+                    WritableMap map;
+                    if (this.usePKCE != null && this.usePKCE && this.codeVerifier != null) {
+                        map = TokenResponseFactory.authorizationCodeResponseToMap(response, this.codeVerifier);
+                    } else {
+                        map = TokenResponseFactory.authorizationResponseToMap(response);
+                    }

-                if (promise != null) {
-                    promise.resolve(map);
+                    if (promise != null) {
+                        promise.resolve(map);
+                    }
+                    return;
                 }
-                return;
-            }


-            final Promise authorizePromise = this.promise;
-            final AppAuthConfiguration configuration = createAppAuthConfiguration(
-                    createConnectionBuilder(this.dangerouslyAllowInsecureHttpRequests, this.tokenRequestHeaders),
-                    this.dangerouslyAllowInsecureHttpRequests,
-                    null
-            );
+                final Promise authorizePromise = this.promise;
+                final AppAuthConfiguration configuration = createAppAuthConfiguration(
+                        createConnectionBuilder(this.dangerouslyAllowInsecureHttpRequests, this.tokenRequestHeaders),
+                        this.dangerouslyAllowInsecureHttpRequests,
+                        null
+                );

-            AuthorizationService authService = new AuthorizationService(this.reactContext, configuration);
+                AuthorizationService authService = new AuthorizationService(this.reactContext, configuration);

-            TokenRequest tokenRequest = response.createTokenExchangeRequest(this.additionalParametersMap);
+                TokenRequest tokenRequest = response.createTokenExchangeRequest(this.additionalParametersMap);

-            AuthorizationService.TokenResponseCallback tokenResponseCallback = new AuthorizationService.TokenResponseCallback() {
+                AuthorizationService.TokenResponseCallback tokenResponseCallback = new AuthorizationService.TokenResponseCallback() {

-                @Override
-                public void onTokenRequestCompleted(
-                        TokenResponse resp, AuthorizationException ex) {
-                    if (resp != null) {
-                        WritableMap map = TokenResponseFactory.tokenResponseToMap(resp, response);
-                        if (authorizePromise != null) {
-                            authorizePromise.resolve(map);
-                        }
-                    } else {
-                        if (promise != null) {
-                            handleAuthorizationException("token_exchange_failed", ex, promise);
+                    @Override
+                    public void onTokenRequestCompleted(
+                            TokenResponse resp, AuthorizationException ex) {
+                        if (resp != null) {
+                            WritableMap map = TokenResponseFactory.tokenResponseToMap(resp, response);
+                            if (authorizePromise != null) {
+                                authorizePromise.resolve(map);
+                            }
+                        } else {
+                            if (promise != null) {
+                                handleAuthorizationException("token_exchange_failed", ex, promise);
+                            }
                         }
                     }
-                }
-            };
+                };

-            if (this.clientSecret != null) {
-                ClientAuthentication clientAuth = this.getClientAuthentication(this.clientSecret, this.clientAuthMethod);
-                authService.performTokenRequest(tokenRequest, clientAuth, tokenResponseCallback);
+                if (this.clientSecret != null) {
+                    ClientAuthentication clientAuth = this.getClientAuthentication(this.clientSecret, this.clientAuthMethod);
+                    authService.performTokenRequest(tokenRequest, clientAuth, tokenResponseCallback);

-            } else {
-                authService.performTokenRequest(tokenRequest, tokenResponseCallback);
-            }
+                } else {
+                    authService.performTokenRequest(tokenRequest, tokenResponseCallback);
+                }

-        }
+            }

-        if (requestCode == 53) {
-            if (data == null) {
-                if (promise != null) {
-                    promise.reject("end_session_failed", "Data intent is null" );
+            if (requestCode == 53) {
+                if (data == null) {
+                    if (promise != null) {
+                        promise.reject("end_session_failed", "Data intent is null" );
+                    }
+                    return;
                 }
-                return;
-            }
-            EndSessionResponse response = EndSessionResponse.fromIntent(data);
-            AuthorizationException ex = AuthorizationException.fromIntent(data);
-            if (ex != null) {
-                if (promise != null) {
-                    handleAuthorizationException("end_session_failed", ex, promise);
+                EndSessionResponse response = EndSessionResponse.fromIntent(data);
+                AuthorizationException ex = AuthorizationException.fromIntent(data);
+                if (ex != null) {
+                    if (promise != null) {
+                        handleAuthorizationException("end_session_failed", ex, promise);
+                    }
+                    return;
                 }
-                return;
+                final Promise endSessionPromise = this.promise;
+                WritableMap map = EndSessionResponseFactory.endSessionResponseToMap(response);
+                endSessionPromise.resolve(map);
+            }
+        } catch (Exception e) {
+            if (promise != null) {
+                promise.reject("run_time_exception", e.getMessage());
+            }
+            else {
+                throw e;
             }
-            final Promise endSessionPromise = this.promise;
-            WritableMap map = EndSessionResponseFactory.endSessionResponseToMap(response);
-            endSessionPromise.resolve(map);
         }
     }

diff --git a/node_modules/react-native-app-auth/index.d.ts b/node_modules/react-native-app-auth/index.d.ts
index aaa5c5e..0d92858 100644
--- a/node_modules/react-native-app-auth/index.d.ts
+++ b/node_modules/react-native-app-auth/index.d.ts
@@ -187,7 +187,8 @@ type AppAuthErrorCode =
   | 'registration_failed'
   | 'browser_not_found'
   | 'end_session_failed'
-  | 'authentication_error';
+  | 'authentication_error'
+  | 'run_time_exception';

 type ErrorCode =
   | OAuthAuthorizationErrorCode

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

react-native-app-auth module getting crashed in Android
3 participants