Firebase sign out not working in Swift

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)
}

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)
        }

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.