firebase-kotlin-sdk
firebase-kotlin-sdk copied to clipboard
Expose iOS MultiFactorResolver to FirebaseAuthMultiFactorException
When a user has been enrolled into multifactor, on sign in, a FirebaseAuthMultiFactorException is thrown, and should contain a MultiFactorResolver. This has a function resolveSignIn to handle entering the code.
On Android for example, the exception contains the resolver, and can be extracted and used successfully.
import com.google.firebase.auth.FirebaseAuthMultiFactorException
import dev.gitlive.firebase.auth.MultiFactorResolver
actual fun extractMultiFactorResolver(e: FirebaseAuthMultiFactorException): MultiFactorResolver? {
return MultiFactorResolver(e.resolver)
}
https://firebase.google.com/docs/auth/android/totp-mfa#sign_in_users_with_a_second_factor
However, on iOS, the resolver isn't included in the exception, and so the code below wouldn't work.
import dev.gitlive.firebase.auth.FirebaseAuthMultiFactorException
import dev.gitlive.firebase.auth.MultiFactorResolver
import kotlinx.cinterop.ExperimentalForeignApi
@OptIn(ExperimentalForeignApi::class)
actual fun extractMultiFactorResolver(e: FirebaseAuthMultiFactorException): MultiFactorResolver? {
val resolver = e.resolver ?: return null
return MultiFactorResolver(resolver)
}
https://firebase.google.com/docs/auth/ios/totp-mfa#sign_in_users_with_a_second_factor
This is because NSError.toException() function throws a new error without exposing the required resolver.
17087L, // AuthErrorCode.secondFactorAlreadyEnrolled
17078L, // AuthErrorCode.secondFactorRequired
17088L, // AuthErrorCode.maximumSecondFactorCountExceeded
17084L, // AuthErrorCode.multiFactorInfoNotFound
-> FirebaseAuthMultiFactorException(toString())
Adding the resolver in the exception allows iOS to complete sign in for users with multi factor enrolled.