From 384b915c8d167e26700029538eae62748ae528f7 Mon Sep 17 00:00:00 2001 From: Mike Hardy Date: Tue, 14 May 2024 21:26:01 -0500 Subject: [PATCH] fix(auth, android): if no credential in collision exception, avoid accessing it --- .../auth/ReactNativeFirebaseAuthModule.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java b/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java index 665203914f..ad1244d04e 100644 --- a/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java +++ b/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java @@ -1617,17 +1617,19 @@ private void linkWithCredential( promiseWithAuthResult(task.getResult(), promise); } else { Exception exception = task.getException(); - if (exception instanceof FirebaseAuthUserCollisionException) { - FirebaseAuthUserCollisionException authUserCollisionException = - (FirebaseAuthUserCollisionException) exception; - AuthCredential updatedCredential = - authUserCollisionException.getUpdatedCredential(); - Log.d(TAG, "link:onComplete:collisionFailure", exception); - promiseRejectLinkAuthException(promise, exception, updatedCredential); - } else { - Log.e(TAG, "link:onComplete:failure", exception); - promiseRejectAuthException(promise, exception); + if (exception instanceof FirebaseAuthUserCollisionException collEx) { + AuthCredential updatedCredential = collEx.getUpdatedCredential(); + Log.d(TAG, "link:onComplete:collisionFailure", collEx); + // If we have a credential in the error, we can return it, otherwise fall + // through + if (updatedCredential != null) { + Log.d(TAG, "link:onComplete:collisionFailure had credential", collEx); + promiseRejectLinkAuthException(promise, collEx, updatedCredential); + return; + } } + Log.e(TAG, "link:onComplete:failure", exception); + promiseRejectAuthException(promise, exception); } }); } else { @@ -2239,7 +2241,9 @@ private void promiseRejectAuthException(Promise promise, Exception exception) { * @param authCredential */ private void promiseRejectLinkAuthException( - Promise promise, Exception exception, AuthCredential authCredential) { + @NonNull Promise promise, + @NonNull Exception exception, + @NonNull AuthCredential authCredential) { WritableMap error = getJSError(exception); String authHashCode = String.valueOf(authCredential.hashCode());