问题描述
我有一个暂停功能,可以通过firebase auth注册用户.
I have a suspend function that register an user with firebase auth.
我知道有auth错误代码,如何将它们放入try cath内?
I know that there are auth error codes, how can i get them inside the try cath?
我当时正在考虑使用 e.message
,然后将其翻译为我的语言,但是我不知道这是否是一个好方法.
I was thinking of using e.message
and then translate them to my language but i dont know if that would be a good approach.
override suspend fun registerUserFirebase(email: String, password: String):Boolean {
return try {
auth.createUserWithEmailAndPassword(email, password).await()
true
} catch (e: Exception) {
if (e.message == "The email address is already in use by another account."){
// Do stuff
}
false
}
}
推荐答案
对于协程,这里没有什么特别的事情要做.根据 createUserWithEmailAndPassword()会产生不同类型的异常:
There's nothing special to do here with respect to coroutines. According to the API docs for createUserWithEmailAndPassword() can yield different types of exceptions:
- 如果密码不够强,就会抛出
- FirebaseAuthWeakPasswordException 如果电子邮件地址格式错误,则会引发
- FirebaseAuthInvalidCredentialsException 如果已经存在具有给定电子邮件地址的帐户,则抛出
- FirebaseAuthUserCollisionException
- FirebaseAuthWeakPasswordException thrown if the password is not strong enough
- FirebaseAuthInvalidCredentialsException thrown if the email address is malformed
- FirebaseAuthUserCollisionException thrown if there already exists an account with the given email address
您的代码将需要使用 is
检查异常的类型,或者可能使用 何时
,并根据自己的意愿确定如何处理每个人.
Your code will need to check the type of exception using is
, or possibly using when
, and determine what to do with each one on its own terms.
这篇关于如何在Kotlin中使用协同程序处理Firebase身份验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!