diff --git a/.changeset/gentle-balloons-march.md b/.changeset/gentle-balloons-march.md new file mode 100644 index 00000000..e484990a --- /dev/null +++ b/.changeset/gentle-balloons-march.md @@ -0,0 +1,5 @@ +--- +'@capacitor-firebase/authentication': patch +--- + +fix(android): `NullPointerException` when `getPendingAuthResult()` is called diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java index b1c372ed..8c204fba 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java @@ -698,59 +698,76 @@ public void handleSuccessfulSignIn( call.resolve(signInResult); return; } - getFirebaseAuthInstance() - .signInWithCredential(credential) - .addOnCompleteListener( - plugin.getActivity(), - task -> { - if (task.isSuccessful()) { - final AuthResult authResult = task.getResult(); - handleSuccessfulSignIn(call, authResult, idToken, nonce, accessToken, serverAuthCode); - } else { - Exception exception = task.getException(); - Logger.error(TAG, exception.getMessage(), exception); - String code = FirebaseAuthenticationHelper.createErrorCode(exception); - call.reject(exception.getMessage(), code); + if (credential == null) { + handleSuccessfulSignIn(call); + } else { + getFirebaseAuthInstance() + .signInWithCredential(credential) + .addOnCompleteListener( + plugin.getActivity(), + task -> { + if (task.isSuccessful()) { + final AuthResult authResult = task.getResult(); + handleSuccessfulSignIn(call, authResult, idToken, nonce, accessToken, serverAuthCode); + } else { + Exception exception = task.getException(); + Logger.error(TAG, exception.getMessage(), exception); + String code = FirebaseAuthenticationHelper.createErrorCode(exception); + call.reject(exception.getMessage(), code); + } } - } - ); + ); + } } public void handleSuccessfulSignIn( final PluginCall call, - final AuthResult authResult, + @Nullable AuthResult authResult, @Nullable String idToken, @Nullable String nonce, @Nullable String accessToken ) { - JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult( - authResult.getUser(), - authResult.getCredential(), - idToken, - nonce, - accessToken, - authResult.getAdditionalUserInfo() - ); - call.resolve(signInResult); + if (authResult == null) { + handleSuccessfulSignIn(call); + } else { + JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult( + authResult.getUser(), + authResult.getCredential(), + idToken, + nonce, + accessToken, + authResult.getAdditionalUserInfo() + ); + call.resolve(signInResult); + } } public void handleSuccessfulSignIn( final PluginCall call, - final AuthResult authResult, + @Nullable AuthResult authResult, @Nullable String idToken, @Nullable String nonce, @Nullable String accessToken, @Nullable String serverAuthCode ) { - JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult( - authResult.getUser(), - authResult.getCredential(), - idToken, - nonce, - accessToken, - serverAuthCode, - authResult.getAdditionalUserInfo() - ); + if (authResult == null) { + handleSuccessfulSignIn(call); + } else { + JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult( + authResult.getUser(), + authResult.getCredential(), + idToken, + nonce, + accessToken, + serverAuthCode, + authResult.getAdditionalUserInfo() + ); + call.resolve(signInResult); + } + } + + public void handleSuccessfulSignIn(@NonNull final PluginCall call) { + JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(null, null, null, null, null, null); call.resolve(signInResult); } @@ -776,41 +793,54 @@ public void handleSuccessfulLink( call.reject(FirebaseAuthenticationPlugin.ERROR_NO_USER_SIGNED_IN); return; } - user - .linkWithCredential(credential) - .addOnCompleteListener( - plugin.getActivity(), - task -> { - if (task.isSuccessful()) { - final AuthResult authResult = task.getResult(); - handleSuccessfulLink(call, authResult, idToken, nonce, accessToken, serverAuthCode); - } else { - Exception exception = task.getException(); - Logger.error(TAG, exception.getMessage(), exception); - String code = FirebaseAuthenticationHelper.createErrorCode(exception); - call.reject(exception.getMessage(), code); + if (credential == null) { + handleSuccessfulLink(call); + } else { + user + .linkWithCredential(credential) + .addOnCompleteListener( + plugin.getActivity(), + task -> { + if (task.isSuccessful()) { + final AuthResult authResult = task.getResult(); + handleSuccessfulLink(call, authResult, idToken, nonce, accessToken, serverAuthCode); + } else { + Exception exception = task.getException(); + Logger.error(TAG, exception.getMessage(), exception); + String code = FirebaseAuthenticationHelper.createErrorCode(exception); + call.reject(exception.getMessage(), code); + } } - } - ); + ); + } } public void handleSuccessfulLink( final PluginCall call, - final AuthResult authResult, + @Nullable AuthResult authResult, @Nullable String idToken, @Nullable String nonce, @Nullable String accessToken, @Nullable String serverAuthCode ) { - JSObject linkResult = FirebaseAuthenticationHelper.createSignInResult( - authResult.getUser(), - authResult.getCredential(), - idToken, - nonce, - accessToken, - serverAuthCode, - authResult.getAdditionalUserInfo() - ); + if (authResult == null) { + handleSuccessfulLink(call); + } else { + JSObject linkResult = FirebaseAuthenticationHelper.createSignInResult( + authResult.getUser(), + authResult.getCredential(), + idToken, + nonce, + accessToken, + serverAuthCode, + authResult.getAdditionalUserInfo() + ); + call.resolve(linkResult); + } + } + + public void handleSuccessfulLink(final PluginCall call) { + JSObject linkResult = FirebaseAuthenticationHelper.createSignInResult(null, null, null, null, null, null); call.resolve(linkResult); }