【问题标题】:How to Properly SignOut Firebase auth after call DocumentSnapshot Firestore?调用 DocumentSnapshot Firestore 后如何正确注销 Firebase 身份验证?
【发布时间】:2019-07-18 04:04:42
【问题描述】:

所以我尝试在片段Home 中创建退出按钮,但是,每次用户退出时,应用程序都会崩溃并且它是由文档快照引起的。

这是我的文档快照代码。

private fun loadProfile() {
    val user = mAuth!!.currentUser
    if (user != null) {
       ...//loading profile code
        }

        val uid = user.uid
        documentReference = db!!.collection("users").document(uid)
        documentReference!!.addSnapshotListener { documentSnapshot, e ->
            //this if returning null after user signout
            if (documentSnapshot!!.exists()) {
                if (documentSnapshot.get(KEY_HP) != null) {
                   ...//loading profile code


            }else {
                val intent = Intent(this@Home.activity, LoginActivity::class.java)
                startActivity(intent)
        }
    }
    } else {
        val intent = Intent(this@Home.activity, LoginActivity::class.java)
        startActivity(intent)
    }
}

如您所见,我拼命地在任何地方添加else,但这无济于事。

这是我的退出方法

private fun signOut() {
        mAuth!!.signOut()
        val intent = Intent(this@Home.activity, LoginActivity::class.java)
        startActivity(intent)


    }

所以,我需要在应用程序不崩溃的情况下正确退出

【问题讨论】:

  • 如果应用程序崩溃,会有一个堆栈跟踪。请在 logcat 上查找,并将其添加到您的问题中。
  • 我已经查过LogCat,这就是我发现if (documentSnapshot!!.exists())在退出后返回null并导致应用程序崩溃,
  • 实际的崩溃是什么?将该消息添加到您的问题中。
  • 也已经写好了,崩溃是由documentSnapshot返回null引起的

标签: android firebase kotlin google-cloud-firestore firebase-authentication


【解决方案1】:
FirebaseAuth.getInstance().signOut();

使用它从 Firebase 中退出。然后,如果您尝试执行已登录用户的任何操作,您将无法执行,您将不得不重新登录。

【讨论】:

    【解决方案2】:

    未来参考

    在与“退出”方法斗争了几天后,我意识到如果您在活动中附加了快照侦听器,请确保您处理并返回错误,如 here 所述,否则您可能会遇到一些权限错误和崩溃。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 2021-05-29
      • 1970-01-01
      相关资源
      最近更新 更多