Firebase sign out not working in Swift

后端 未结 3 793
傲寒
傲寒 2021-01-01 18:12

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.viewDi         


        
相关标签:
3条回答
  • 2021-01-01 18:50

    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)
            }
    
    0 讨论(0)
  • 2021-01-01 18:50

    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.

    0 讨论(0)
  • 2021-01-01 19:09

    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)
    }
    
    0 讨论(0)
提交回复
热议问题