Android firebase auth 同步登录

标签 android firebase firebase-authentication

当我们希望代码在登录 Firebase 后运行时,我们使用 addOnCompleteListener 像这样:

void signInToFirebaseUsing(GoogleSignInAccount googleAccount) {
    AuthCredential credential = GoogleAuthProvider.getCredential(googleAccount.getIdToken(), null)
    firebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener { ...
                if (task.isSuccessful()) {
                    ...                     
                } else {
                    ...
                }
            }
}

但是,firebase 并不是我想要登录的唯一服务。我需要同步 firebase 登录,以便我可以处理像这样的多个身份验证(我将 Kotlin 与 AsyncAwait library 结合使用,但您明白了):

private fun signInToAppUsing(googleAccount: GoogleSignInAccount) {
    async {
        try {
            await { signInToRealmUsing(googleAccount) }
            await { signInToFirebaseUsing(googleAccount) } // <- this won't work because firebase uses asynchronous listener
            // User is signed in all services
        } catch (e: Exception) {
            // Signing in some service crashed
        }
    }
}

有办法做到这一点吗?

最佳答案

已经很晚了,但今天我遇到了这个问题,只想与其他人分享。我在互联网上查找并找到了一个有趣的解决方案。

因为我使用RxFirebase (这个版本兼容RxJava 1。如果您使用RxJava 2,请使用 Rx2Firebase ),所以我有一个简单的方法来解决这个问题。只需一行代码:

FirebaseUser user = RxFirebaseAuth.signInWithCredential(
    FirebaseAuth.getInstance(), credential
).toBlocking().value();

如果您使用 Kotlin 协程,请使用 this扩展然后:

FirebaseAuth.getInstance().signInWithCredential(credential).await()

当然,这个任务应该在后台线程上启动。

关于Android firebase auth 同步登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42473048/

相关文章:

android - 如何在 Android 8 上获得广播 ACTION_POWER_CONNECTED

android - 在 Android SQLite 中使用 COLLATE - 在 LIKE 语句中忽略语言环境

javascript - 如何验证电子邮件在 Firebase 中的真实性?

javascript - 在 Firebase 中,如何在 Auth 中更新用户的 displayName 字段?

javascript - firebase.auth().currentUser 和 onAuthStateChanged 始终为 null,尽管已登录

android - 服务器选项卡在 eclipse v3.7.2 中不可见

android - React-Native 更改 iOS 和 Android 文件夹的默认位置

javascript - 如何在 Cloud Functions 中解析/返回 forEach 函数

android - 如何在 Kotlin 中获取 Android 设备 token 以用于 Firebase 云消息传递

ios - Firebase QueryOrderedByChild 以及 EqualToValue 和 StartingAtValue 组合