Skip to content

Commit 7f46adc

Browse files
bobsmitsBob Smits
andauthored
fix (Android) Correctly parse AuthorizationException type other to javascript side (#646)
AuthorizationExceptions from non standard oauth errors for example when the server returns a custom response do not have a exception.getLocalizedMessage() but only put the server response in exeption.error. With this fix, the Error object on the javascript is equal for iOS and Android. Co-authored-by: Bob Smits <bob.smits@weareyou.com>
1 parent 0f97aca commit 7f46adc

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

android/src/main/java/com/rnappauth/RNAppAuthModule.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,10 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
403403
}
404404

405405
final AuthorizationResponse response = AuthorizationResponse.fromIntent(data);
406-
AuthorizationException exception = AuthorizationException.fromIntent(data);
407-
if (exception != null) {
406+
AuthorizationException ex = AuthorizationException.fromIntent(data);
407+
if (ex != null) {
408408
if (promise != null) {
409-
promise.reject(exception.error != null ? exception.error: "authentication_error", exception.getLocalizedMessage(), exception);
409+
handleAuthorizationException("authentication_error", ex, promise);
410410
}
411411
return;
412412
}
@@ -447,7 +447,7 @@ public void onTokenRequestCompleted(
447447
}
448448
} else {
449449
if (promise != null) {
450-
promise.reject(ex.error != null ? ex.error: "token_exchange_failed", ex.getLocalizedMessage(), ex);
450+
handleAuthorizationException("token_exchange_failed", ex, promise);
451451
}
452452
}
453453
}
@@ -514,7 +514,7 @@ public void onRegistrationRequestCompleted(@Nullable RegistrationResponse respon
514514
WritableMap map = RegistrationResponseFactory.registrationResponseToMap(response);
515515
promise.resolve(map);
516516
} else {
517-
promise.reject(ex.error != null ? ex.error: "registration_failed", ex.getLocalizedMessage(), ex);
517+
handleAuthorizationException("registration_failed", ex, promise);
518518
}
519519
}
520520
};
@@ -652,7 +652,7 @@ public void onTokenRequestCompleted(@Nullable TokenResponse response, @Nullable
652652
WritableMap map = TokenResponseFactory.tokenResponseToMap(response);
653653
promise.resolve(map);
654654
} else {
655-
promise.reject(ex.error != null ? ex.error: "token_refresh_failed", ex.getLocalizedMessage(), ex);
655+
handleAuthorizationException("token_refresh_failed", ex, promise);
656656
}
657657
}
658658
};
@@ -822,6 +822,14 @@ private AuthorizationServiceConfiguration getServiceConfiguration(@Nullable Stri
822822
}
823823
}
824824

825+
private void handleAuthorizationException(final String fallbackErrorCode, final AuthorizationException ex, final Promise promise) {
826+
if (ex.getLocalizedMessage() == null) {
827+
promise.reject(fallbackErrorCode, ex.error, ex);
828+
} else {
829+
promise.reject(ex.error != null ? ex.error: fallbackErrorCode, ex.getLocalizedMessage(), ex);
830+
}
831+
}
832+
825833
private void setServiceConfiguration(@Nullable String issuer, AuthorizationServiceConfiguration serviceConfiguration) {
826834
if (issuer != null) {
827835
mServiceConfigurations.put(issuer, serviceConfiguration);

0 commit comments

Comments
 (0)