FirebaseAuth - how can I wait for value(Firebase Auth-如何等待价值)
问题描述
我使用Firebase Auth注册新用户
class FirebaseAuthenticationServiceImpl(): FirebaseAuthenticationService {
override fun registerWithEmailAndPassword(email: String, password: String): Boolean {
val registration = FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnSuccessListener {
println(it.additionalUserInfo?.providerId.toString())
}.addOnFailureListener {
println(it.message.toString())
}
return registration.isSuccessful
}
}
我调用上面的函数,每次我得到false
。一段时间后,我收到true
coroutineScope {
try {
if (firebaseService.registerWithEmailAndPassword(email, password)) {
openHomeActivity.offer(Unit)
} else {}
} catch (e: Exception) {}
}
如何等待第u个结果(成功/失败)并获得该值?
推荐答案
防火墙身份验证服务来自哪里?你需要它吗?官方入门指南只使用Firebase.auth
。这样,您就可以使用await()
暂停函数而不是使用回调方法进行身份验证。
// In a coroutine:
val authResult = Firebase.auth.registerWithEmailAndPassword(email, password).await()
val user: FirebaseUser = authResult.user
if (user != null) {
openHomeActivity.offer(Unit)
} else {
// authentication failed
}
这篇关于Firebase Auth-如何等待价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!