Firebase Auth-如何等待价值 [英] FirebaseAuth - how can I wait for value

查看:128
本文介绍了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-如何等待价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆