Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase sign out not working in Swift

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?

like image 488
Dejan Avatar asked Jun 21 '16 11:06

Dejan


3 Answers

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)
        }
like image 54
Damien Bannerot Avatar answered Oct 07 '22 13:10

Damien Bannerot


2020 ...

do { try Auth.auth().signOut() }
catch { print("already logged out") }

Typically, on your "base" screen, something like ...

func logoutUser() {
    // call from any screen
    
    do { try Auth.auth().signOut() }
    catch { print("already logged out") }
    
    navigationController?.popToRootViewController(animated: true)
}
like image 26
Fattie Avatar answered Oct 07 '22 12:10

Fattie


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.

like image 23
Adam Webb Avatar answered Oct 07 '22 12:10

Adam Webb