Skip to content

Commit 935c46c

Browse files
Fix/expose Firebase Auth error codes and map password errors (#823)
* expose firebase auth error codes across platforms * map apple wrong password auth errors to invalid credentials * update firebase auth api snapshots * map js wrong password auth errors to invalid credentials --------- Co-authored-by: Nicholas Bransby-Williams <nbransby@gmail.com>
1 parent 44bb4a3 commit 935c46c

8 files changed

Lines changed: 48 additions & 21 deletions

File tree

firebase-auth/api/android/firebase-auth.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,6 @@ public final class dev/gitlive/firebase/auth/android {
296296
public static final fun getAndroid (Ldev/gitlive/firebase/auth/AuthTokenResult;)Lcom/google/firebase/auth/GetTokenResult;
297297
public static final fun getAndroid (Ldev/gitlive/firebase/auth/FirebaseAuth;)Lcom/google/firebase/auth/FirebaseAuth;
298298
public static final fun getAuth (Ldev/gitlive/firebase/Firebase;)Ldev/gitlive/firebase/auth/FirebaseAuth;
299+
public static final fun getCode (Lcom/google/firebase/auth/FirebaseAuthException;)Ljava/lang/String;
299300
}
300301

firebase-auth/api/jvm/firebase-auth.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,5 +294,6 @@ public final class dev/gitlive/firebase/auth/android {
294294
public static final fun getAndroid (Ldev/gitlive/firebase/auth/AuthResult;)Lcom/google/firebase/auth/AuthResult;
295295
public static final fun getAndroid (Ldev/gitlive/firebase/auth/AuthTokenResult;)Lcom/google/firebase/auth/GetTokenResult;
296296
public static final fun getAuth (Ldev/gitlive/firebase/Firebase;)Ldev/gitlive/firebase/auth/FirebaseAuth;
297+
public static final fun getCode (Lcom/google/firebase/auth/FirebaseAuthException;)Ljava/lang/String;
297298
}
298299

firebase-auth/src/androidMain/kotlin/dev/gitlive/firebase/auth/auth.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ internal fun ActionCodeSettings.toAndroid() = com.google.firebase.auth.ActionCod
165165
.build()
166166

167167
public actual typealias FirebaseAuthException = com.google.firebase.auth.FirebaseAuthException
168+
public actual val FirebaseAuthException.code: String? get() = errorCode
168169
public actual typealias FirebaseAuthActionCodeException = com.google.firebase.auth.FirebaseAuthActionCodeException
169170
public actual typealias FirebaseAuthEmailException = com.google.firebase.auth.FirebaseAuthEmailException
170171
public actual typealias FirebaseAuthInvalidCredentialsException = com.google.firebase.auth.FirebaseAuthInvalidCredentialsException

firebase-auth/src/appleMain/kotlin/dev/gitlive/firebase/auth/auth.kt

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,17 @@ internal fun ActionCodeSettings.toIos() = FIRActionCodeSettings().also {
160160
iOSBundleId?.run { it.setIOSBundleID(this) }
161161
}
162162

163-
public actual open class FirebaseAuthException(message: String) : FirebaseException(message)
164-
public actual open class FirebaseAuthActionCodeException(message: String) : FirebaseAuthException(message)
165-
public actual open class FirebaseAuthEmailException(message: String) : FirebaseAuthException(message)
166-
public actual open class FirebaseAuthInvalidCredentialsException(message: String) : FirebaseAuthException(message)
167-
public actual open class FirebaseAuthWeakPasswordException(message: String) : FirebaseAuthInvalidCredentialsException(message)
168-
public actual open class FirebaseAuthInvalidUserException(message: String) : FirebaseAuthException(message)
169-
public actual open class FirebaseAuthMultiFactorException(message: String, public val resolver: FIRMultiFactorResolver?) : FirebaseAuthException(message)
170-
public actual open class FirebaseAuthRecentLoginRequiredException(message: String) : FirebaseAuthException(message)
171-
public actual open class FirebaseAuthUserCollisionException(message: String) : FirebaseAuthException(message)
172-
public actual open class FirebaseAuthWebException(message: String) : FirebaseAuthException(message)
163+
public actual open class FirebaseAuthException(message: String, internal val authCode: String? = null) : FirebaseException(message)
164+
public actual val FirebaseAuthException.code: String? get() = authCode
165+
public actual open class FirebaseAuthActionCodeException(message: String, code: String? = null) : FirebaseAuthException(message, code)
166+
public actual open class FirebaseAuthEmailException(message: String, code: String? = null) : FirebaseAuthException(message, code)
167+
public actual open class FirebaseAuthInvalidCredentialsException(message: String, code: String? = null) : FirebaseAuthException(message, code)
168+
public actual open class FirebaseAuthWeakPasswordException(message: String, code: String? = null) : FirebaseAuthInvalidCredentialsException(message, code)
169+
public actual open class FirebaseAuthInvalidUserException(message: String, code: String? = null) : FirebaseAuthException(message, code)
170+
public actual open class FirebaseAuthMultiFactorException(message: String, public val resolver: FIRMultiFactorResolver?, code: String? = null) : FirebaseAuthException(message, code)
171+
public actual open class FirebaseAuthRecentLoginRequiredException(message: String, code: String? = null) : FirebaseAuthException(message, code)
172+
public actual open class FirebaseAuthUserCollisionException(message: String, code: String? = null) : FirebaseAuthException(message, code)
173+
public actual open class FirebaseAuthWebException(message: String, code: String? = null) : FirebaseAuthException(message, code)
173174

174175
internal fun <T, R> T.throwError(block: T.(errorPointer: CPointer<ObjCObjectVar<NSError?>>) -> R): R {
175176
memScoped {
@@ -214,10 +215,10 @@ private fun NSError.toException() = when (domain) {
214215
FIRAuthErrorDomain -> when (code) {
215216
17030L, // AuthErrorCode.invalidActionCode
216217
17029L, // AuthErrorCode.expiredActionCode
217-
-> FirebaseAuthActionCodeException(toString())
218+
-> FirebaseAuthActionCodeException(toString(), code.toString())
218219

219220
17008L, // AuthErrorCode.invalidEmail
220-
-> FirebaseAuthEmailException(toString())
221+
-> FirebaseAuthEmailException(toString(), code.toString())
221222

222223
17056L, // AuthErrorCode.captchaCheckFailed
223224
17042L, // AuthErrorCode.invalidPhoneNumber
@@ -228,24 +229,25 @@ private fun NSError.toException() = when (domain) {
228229
17043L, // AuthErrorCode.missingVerificationCode
229230
17021L, // AuthErrorCode.userTokenExpired
230231
17004L, // AuthErrorCode.invalidCredential
231-
-> FirebaseAuthInvalidCredentialsException(toString())
232+
17009L, // AuthErrorCode.wrongPassword
233+
-> FirebaseAuthInvalidCredentialsException(toString(), code.toString())
232234

233235
17026L, // AuthErrorCode.weakPassword
234-
-> FirebaseAuthWeakPasswordException(toString())
236+
-> FirebaseAuthWeakPasswordException(toString(), code.toString())
235237

236238
17017L, // AuthErrorCode.invalidUserToken
237-
-> FirebaseAuthInvalidUserException(toString())
239+
-> FirebaseAuthInvalidUserException(toString(), code.toString())
238240

239241
17014L, // AuthErrorCode.requiresRecentLogin
240-
-> FirebaseAuthRecentLoginRequiredException(toString())
242+
-> FirebaseAuthRecentLoginRequiredException(toString(), code.toString())
241243

242244
17087L, // AuthErrorCode.secondFactorAlreadyEnrolled
243245
17078L, // AuthErrorCode.secondFactorRequired
244246
17088L, // AuthErrorCode.maximumSecondFactorCountExceeded
245247
17084L, // AuthErrorCode.multiFactorInfoNotFound
246248
-> {
247249
val resolver = userInfo["FIRAuthErrorUserInfoMultiFactorResolverKey"] as? FIRMultiFactorResolver
248-
FirebaseAuthMultiFactorException(toString(), resolver)
250+
FirebaseAuthMultiFactorException(toString(), resolver, code.toString())
249251
}
250252

251253
17052L, // AuthErrorCode.quotaExceeded
@@ -254,17 +256,17 @@ private fun NSError.toException() = when (domain) {
254256
17007L, // AuthErrorCode.emailAlreadyInUse
255257
17012L, // AuthErrorCode.accountExistsWithDifferentCredential
256258
17025L, // AuthErrorCode.credentialAlreadyInUse
257-
-> FirebaseAuthUserCollisionException(toString())
259+
-> FirebaseAuthUserCollisionException(toString(), code.toString())
258260

259261
17057L, // AuthErrorCode.webContextAlreadyPresented
260262
17058L, // AuthErrorCode.webContextCancelled
261263
17062L, // AuthErrorCode.webInternalError
262-
-> FirebaseAuthWebException(toString())
264+
-> FirebaseAuthWebException(toString(), code.toString())
263265

264266
17020L, // AuthErrorCode.networkError
265267
-> FirebaseNetworkException(toString())
266268

267-
else -> FirebaseAuthException(toString())
269+
else -> FirebaseAuthException(toString(), code.toString())
268270
}
269271
else -> FirebaseAuthException(toString())
270272
}

firebase-auth/src/commonMain/kotlin/dev/gitlive/firebase/auth/auth.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public data class AndroidPackageName(
8787
)
8888

8989
public expect open class FirebaseAuthException : FirebaseException
90+
91+
/** Platform Firebase Auth error code, when the underlying SDK provides one. */
92+
public expect val FirebaseAuthException.code: String?
9093
public expect class FirebaseAuthActionCodeException : FirebaseAuthException
9194
public expect class FirebaseAuthEmailException : FirebaseAuthException
9295
public expect open class FirebaseAuthInvalidCredentialsException : FirebaseAuthException

firebase-auth/src/commonTest/kotlin/dev/gitlive/firebase/auth/auth.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ class FirebaseAuthTest {
5555
assertEquals(uid, result.user!!.uid)
5656
}
5757

58+
@Test
59+
fun testSignInWithWrongPasswordThrowsInvalidCredentialsWithCode() = runTest {
60+
val email = "test+${Random.nextInt(100000)}@test.com"
61+
auth.createUserWithEmailAndPassword(email, "test123")
62+
try {
63+
auth.signOut()
64+
65+
val exception = assertFailsWith<FirebaseAuthInvalidCredentialsException> {
66+
auth.signInWithEmailAndPassword(email, "wrong-password")
67+
}
68+
assertNotNull(exception.code)
69+
} finally {
70+
auth.signInWithEmailAndPassword(email, "test123").user!!.delete()
71+
}
72+
}
73+
5874
@Test
5975
fun testCreateUserWithEmailAndPassword() = runTest {
6076
val email = "test+${Random.nextInt(100000)}@test.com"

firebase-auth/src/jsMain/kotlin/dev/gitlive/firebase/auth/auth.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ internal fun ActionCodeSettings.toJson() = json(
183183
"ios" to (iOSBundleId?.run { json("bundleId" to iOSBundleId) } ?: undefined),
184184
)
185185

186-
public actual open class FirebaseAuthException(code: String?, cause: Throwable) : FirebaseException(code, cause)
186+
public actual open class FirebaseAuthException(internal val authCode: String?, cause: Throwable) : FirebaseException(authCode, cause)
187+
public actual val FirebaseAuthException.code: String? get() = authCode
187188
public actual open class FirebaseAuthActionCodeException(code: String?, cause: Throwable) : FirebaseAuthException(code, cause)
188189
public actual open class FirebaseAuthEmailException(code: String?, cause: Throwable) : FirebaseAuthException(code, cause)
189190
public actual open class FirebaseAuthInvalidCredentialsException(code: String?, cause: Throwable) : FirebaseAuthException(code, cause)
@@ -220,6 +221,7 @@ private fun errorToException(cause: dynamic) = when (val code = cause.code?.toSt
220221
"auth/missing-verification-code",
221222
"auth/invalid-verification-id",
222223
"auth/missing-verification-id",
224+
"auth/wrong-password",
223225
-> FirebaseAuthInvalidCredentialsException(code, cause.unsafeCast<Throwable>())
224226
"auth/maximum-second-factor-count-exceeded",
225227
"auth/second-factor-already-in-use",

firebase-auth/src/jvmMain/kotlin/dev/gitlive/firebase/auth/auth.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ internal fun ActionCodeSettings.toAndroid() = com.google.firebase.auth.ActionCod
168168
.build()
169169

170170
public actual typealias FirebaseAuthException = com.google.firebase.auth.FirebaseAuthException
171+
public actual val FirebaseAuthException.code: String? get() = errorCode
171172
public actual typealias FirebaseAuthActionCodeException = com.google.firebase.auth.FirebaseAuthActionCodeException
172173
public actual typealias FirebaseAuthEmailException = com.google.firebase.auth.FirebaseAuthEmailException
173174
public actual typealias FirebaseAuthInvalidCredentialsException = com.google.firebase.auth.FirebaseAuthInvalidCredentialsException

0 commit comments

Comments
 (0)