【问题标题】:How would I be able to detect if the user still has an auth session in firebase?我如何能够检测到用户在 firebase 中是否仍有身份验证会话?
【发布时间】:2019-03-12 02:40:48
【问题描述】:

我能够找到这个:How to check if user has valid Auth Session Firebase iOS?,但它无法回答我的问题。

我正在尝试做的事情:我知道除非用户删除自己的帐户,否则这永远不会发生,但我知道用户是否仍有身份验证会话吗?

例如,在身份验证选项卡下,我删除了测试用户创建的条目,即使我使用了这些代码:

在 AppDelegate.swift 中:

if Auth.auth().currentUser == nil {
        try! Auth.auth().signOut()
}

在我的主视图控制器中:

private func checkIfSignedIn() {
    Auth.auth().addStateDidChangeListener { (auth, user) in
        if user != nil {
            // code to stay signed in
        } else {
            // code for dismissing the controller and moving to login controller
        }
    }
}

当我从 firebase 控制台的身份验证部分运行擦除条目并重新运行应用程序时,用户仍处于登录状态并且仍然具有有效的 UID(我能够打印它以进行检查)。

它检测到何时应该退出并且不再重新登录的唯一时间是当我手动单击退出按钮时。有没有办法解决这个问题?

【问题讨论】:

标签: ios swift firebase firebase-authentication


【解决方案1】:

你没有打电话给StateDidChangeListener。相反,您是在告诉 Firebase 在身份验证状态更改时调用您,这可能会在重新加载页面时发生几次。

当页面正在加载并且之前有用户登录时,身份验证状态可能会更改几次,而客户端正在确定用户的身份验证状态是否仍然有效。如果您已经登录,Auth.auth().currentUser 将始终返回一个有效用户。所以请使用它。

private func checkIfSignedIn() {
   if Auth.auth().currentUser != nil {
      // code to stay signed in
   }else{
      // redirect to sign in page
   }
}

希望对你有帮助。

【讨论】:

    【解决方案2】:

    看看这个:https://firebase.google.com/docs/auth/ios/manage-users#re-authenticate_a_user

    我不是 IOS 开发人员,但从我从文档中读到的内容来看,您的代码中可能缺少某些内容,并且与 WillB 指出的 Stackoverflow 答案中的内容非常相似:How can I detect if the user was deleted from Firebase auth?

    【讨论】:

      【解决方案3】:

      您想对 AppDelegate 中的代码做什么?当然,如果currentUsernil,那么你已经退出了,这意味着这段代码永远不会做任何事情?

      如果这是出于测试目的,并且您只是想测试listener,那么您需要在退出前登录,即在 AppDelegate 中将其更改为if Auth.auth().currentUser != nil

      【讨论】:

        猜你喜欢
        • 2016-10-10
        • 2013-03-02
        • 2013-06-24
        • 1970-01-01
        • 2017-03-08
        • 1970-01-01
        • 2018-01-11
        • 2018-12-28
        • 2017-09-11
        相关资源
        最近更新 更多