Firebase sign out not working in Swift

故事扮演 提交于 2019-12-20 18:15:22

问题


I am using newest Firebase API (3.2.1) and I am using this code to check if user is signed in:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    if(self.navigationController != nil){
        self.navigationController!.setNavigationBarHidden(true, animated: true)
    }

    if(FIRAuth.auth() != nil){
        self.performSegueWithIdentifier("loginSuccessSegue", sender: self)
    }
}

In other words if auth object is present I am switching to other controller. On that controller I have sign out button which is doing sign out like this:

do{
    try FIRAuth.auth()?.signOut()
    self.performSegueWithIdentifier("logoutSegue", sender: self)
}catch{
    print("Error while signing out!")
}

I do not get error on this operation but when I am switched to login controller, this auth object is present and I get switched back again to controller with data. I also tried checking the current user object in auth and it is present and valid.

Anyone knows how an I properly do sign out?


回答1:


try using :

try! FIRAuth.auth()!.signOut()

This is the code I have in an IBAction, and it's working just fine :

try! FIRAuth.auth()!.signOut()     
if let storyboard = self.storyboard {
    let vc = storyboard.instantiateViewControllerWithIdentifier("firstNavigationController") as! UINavigationController
        self.presentViewController(vc, animated: false, completion: nil)
    }

Swift 4.2 Update #

try! Auth.auth().signOut()

if let storyboard = self.storyboard {
            let vc = storyboard.instantiateViewController(withIdentifier: "firstNavigationController") as! UINavigationController
            self.present(vc, animated: false, completion: nil)
        }



回答2:


I'd just like to add that I had the same problem and have been trying various combinations of the code that others have suggested.

The problem for me turned out to be that when I set up my Logout button in the storyboard, I also created a connection by control dragging from the button to my login view controller, thinking that that was what I wanted it to do.

It turned out that my signout code never ran because of the Triggered Segue back to login controller, so it went back to the login screen and immediately to my second view controller because the user was never logged out.

So in the end this worked for me:

do {
    try Auth.auth().signOut()
    self.dismiss(animated: true, completion: nil)
    } catch let err {
        print(err)
}

But only after I deleted the segue I had unwittingly created.



来源:https://stackoverflow.com/questions/37943616/firebase-sign-out-not-working-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!