title: Login & logout problems in Firebase
body: <p>I am dealing with this problem for 2 days now. I am having a lot of trouble correctly signing a user in and out. I am running a function that will print the user?.uid every second, so I can see if it goes correctly.</p>
  
  <p>Problem signing in and out are similar: when signing in, I still see "nil" in my debug session every second. When I close the app, so double tap home screen and remove the app, and reopen the app, the user?.uid is showing correctly because it automatically logs in. So my question is here: What is wrong with this code, and why won't it sign in correctly the first time? This is the code for Facebook authentication:</p>
  
  <pre><code>func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
      if error != nil {
          print(error)
          return
      }
      else if result.isCancelled
      {
  
      }else{
          print("no error, trying to sign in on facebook")
      let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
      FIRAuth.auth()?.signIn(with: credential) { (user, error) in
          print(error)
          if error == nil{
          print("trying to sign into firebase with facebook")
          loggedIn = true
          self.tryLogin()
          self.removeRegisterView()
          self.ref = FIRDatabase.database().reference()
          self.ref.child("users").child("\(FIRAuth.auth()!.currentUser!.uid)").observeSingleEvent(of: .value, with: { (snapshot) in
              if snapshot.hasChild("email"){
              }else{
                  print("user has no email setted")
                  if let user = user {
                      let user = FIRAuth.auth()?.currentUser
                      let email = user?.email
                      self.ref.child("users/\(user!.uid)/email").setValue(email){ (error, ref) -&gt; Void in
                          //self.setUpNewUser()
                      }
                  }
              }
          })
          }
      }
      }
  }
  </code></pre>
  
  <p>Code for email and password account creation:</p>
  
  <pre><code> FIRAuth.auth()?.createUser(withEmail: registerEmail.text!, password: registerPassword.text!) { (user, error) -&gt; Void in
  
                  if error == nil {
                      user!.sendEmailVerification(completion: nil) }
  </code></pre>
  
  <p>This should correctly login the user right? My debug still says "nil" every second. Again: closing the app and reopening the app will fix this problem, by displaying the correct user?.uid. But I want it of course to run correctly the first time.</p>
  
  <p>Signing out has the similar effect: When clicking the logout button this is my code:</p>
  
  <pre><code>@IBAction func logOut(_ sender: UIButton) {
      guard FIRAuth.auth()?.currentUser != nil else {
          return
      }
  
      do {
          print("user's acces token is \(FBSDKAccessToken.current())")
          print("user's UID is \(user?.uid)")
          print("trying to sign out")
          FBSDKAccessToken.setCurrent(nil)
          try FIRAuth.auth()!.signOut()
          let loginManager = FBSDKLoginManager()
          loginManager.logOut()
          print("New user value's:")
          print("user's acces token is \(FBSDKAccessToken.current())")
          print("user's UID is \(user?.uid)")
          loggedIn = false
          storedValuesData.setValue(nil, forKey: "savedLoginEmail")
          storedValuesData.setValue(nil, forKey: "savedLoginPassword")
          JumpToVC()
      } catch let error as NSError {
          print(error.localizedDescription)
      }
  }
  </code></pre>
  
  <p>This is my debug saying:</p>
  
  <p>user's acces token is Optional()</p>
  
  <p>user's UID is Optional("C8rBXwFTejSCAoVmV4EhNRZ6a9n1")</p>
  
  <p>trying to sign out</p>
  
  <p>New user value's:</p>
  
  <p>user's acces token is nil</p>
  
  <p>user's UID is Optional("C8rBXwFTejSCAoVmV4EhNRZ6a9n1")</p>
  
  <p>It wont logout correctly, with as a result, my debug gets filled with the user's UID. Again: closing that app and reopening it would fix it, by displaying nil.</p>
  
  <p>How can it be that I need to close and reopen my app every time to sign in and sign out properly for Facebook? What am I doing wrong?</p>
  
  <p>UPDATE: O wow this is weird. I fixed it. First I had this line of code in an other VC with other values the other VC's can refer to: let user = FIRAuth.auth()?.currentUser
  I deleted that line of code and added it to every function separately. now it works. And this took me hours -_-</p>