【问题标题】:Firebase signOut not performing as expectedFirebase 签出未按预期执行
【发布时间】:2017-12-06 22:39:00
【问题描述】:

我在使用 Firebase 的 iOS 应用时遇到问题。我正在使用以下代码退出:

     @IBAction func logoutDidTap(_ sender: Any) {
            try! FIRAuth.auth()?.signOut()

虽然应用程序 UI 反映了一个 signOut,但我收到以下控制台消息: [Firebase/Database][I-RDB03812] /media 的侦听器失败:permission_denied 此外,当我使用不同的用户凭据登录时,该应用程序将以旧用户身份登录。

    override func viewDidAppear(_ animated: Bool) {        

    //user logged in?
    FIRAuth.auth()?.addStateDidChangeListener({ (auth, user) in
        if let user = user {    //signed in
            DatabaseReference.users(uid: user.uid).reference().observeSingleEvent(of: .value, with: { (snapshot) in
                if let userDict = snapshot.value as? [String: Any] {
                    self.currentUser = User(dictionary: userDict)
                }
            })
        }else {
            self.performSegue(withIdentifier: Storyboard.showWelcome, sender: nil)
        }
    })

有什么想法吗?

【问题讨论】:

  • 很可能存在这样一种情况,即注销被推入堆栈并在其他侦听器之前弹出;因此应用程序在退出后尝试读取 Firebase 节点;因此许可被拒绝。这可能是您有一个顶级观察者并且其他事件正在该闭包内发生的情况,包括注销功能。
  • 您需要返回旧问题并将答案标记为正确或提供反馈。只提出问题而不向给你答案的人提供反馈是不好的形式。

标签: ios swift firebase firebase-authentication


【解决方案1】:

我不知道它是否有帮助,但我所做的是在 AppDelegate 中添加 addStateDidChangeListener 以检查当前用户是否已登录。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    FIRApp.configure()
    handle = FIRAuth.auth()?.addStateDidChangeListener({ (auth, user) in
        if FIRAuth.auth()?.currentUser != nil{
           // go to main screen if there is a user logged in

        }else{

           //go to login screen

        }

    })


    return true
}

【讨论】:

    【解决方案2】:

    我将此代码添加到didFinishLaunchingWithOptions 下的 AppDelegate 中。如果用户已登录,它会跳过视图控制器。

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
            FIRApp.configure()
    
    // MARK: Skip splash screen and login, if user is logged in
            storyboard =  UIStoryboard(name: "Main", bundle: Bundle.main)
            let currentUser = FIRAuth.auth()?.currentUser
            if currentUser != currentUser
            {
                self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
            }
            else
            {
                self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "ViewController")
            }
            return true
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-20
      • 2017-12-15
      • 2011-02-25
      • 2012-11-23
      • 2014-10-20
      • 2015-09-17
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多