2

This question is asked before: Firebase sign out not working in Swift Logging a user out with Firebase 3 and Swift still shows the `currentUser` Firebase - iOS Swift: FIRAuth.auth().signOut() not signing out current user

However all the answers are not working for me. I have VC 1 where the user can login, and in the viewdidappear I have a print that prints the user?.uid. In VC 2 I have a button that should logout the user and goes back to VC 1. The code in VC 2:

@IBAction func logOut(_ sender: UIButton) {
    if FIRAuth.auth()?.currentUser != nil {
        do {
           try! FIRAuth.auth()!.signOut()

        } catch let error as NSError {
            print(error.localizedDescription)
        }
    }
    FBSDKAccessToken.setCurrent(nil)
    loggedIn = false
    storedValuesData.setValue(nil, forKey: "savedLoginEmail")
    storedValuesData.setValue(nil, forKey: "savedLoginPassword")
    jumpToVC1()
}

When I am back at VC 1, when the user pressed the logout button, the print prints again the user's UID. But that user should be logged out, therefore the print should be nil. How can I make sure VC 1 only gets presented when I am sure the user is logged out? I thought completion blocks would be nice, but I am not sure how that would work here...

Community
  • 1
  • 1
Petravd1994
  • 893
  • 1
  • 8
  • 24

2 Answers2

6

You don't need completion blocks, as the method is synchronous.

@IBAction func logOut(_ sender: UIButton) {
    guard FIRAuth.auth()?.currentUser != nil else {
        return
    }            

    do {
       try FIRAuth.auth()?.signOut()
       FBSDKAccessToken.setCurrent(nil)
       loggedIn = false
       storedValuesData.setValue(nil, forKey: "savedLoginEmail")
       storedValuesData.setValue(nil, forKey: "savedLoginPassword")
       jumpToVC1()
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}
Alistra
  • 5,177
  • 2
  • 30
  • 42
0

AccessToken.current=nil is worked.Because logout is not reverse it.

  do {
              try Auth.auth().signOut()
              AccessToken.current=nil


              return true
            } catch {
              return false
            }
Erhan Demirci
  • 4,173
  • 4
  • 36
  • 44