bogdan barbulescu bogdan barbulescu - 1 month ago 41x
Swift Question

Firebase user can not be logged out - Swift

  1. I run the app in Xcode simulator and log in with user and password then I log out, the
    are hidden and
    button shows. So far it behaves as expected.

    • 2

Next, stop the app from Xcode and run it again, but now it behaves as if I was already logged in: shows
,bookingsOutlet, hides logInOutlet. It behaves in the same when the app is suspended, terminated or deleted. ( i.e, Stop in Xcode or cmd + H * 2 and swipe up.)
Now, assuming that I have an active logIn session in the next ViewController I try to retrieve data from FireBase Realtime DataBase, but I get this error.
Error Domain=com.firebase Code=1 "Permission Denied" UserInfo={NSLocalizedDescription=Permission Denied}. Why can I not log out completely?

@IBAction func logOut(sender: AnyObject) {
// if the current user is logged in, try to log out and if logout is successful:
// hide: logOut button & bookings button
// show: logIn button
if FIRAuth.auth() != nil {

do {
try FIRAuth.auth()?.signOut()

print("the user is logged out")
} catch let error as NSError {
print("the current user id is \(FIRAuth.auth()?.currentUser?.uid)")

self.logInOutlet.hidden = false
self.logOutOutlet.hidden = true
self.bookingsOutlet.hidden = true
} // end of if.. FIRAuth.auth()

override func viewDidLoad() {

// if the user is logged out
// hide: logOut button & bookings button
// show: logIn button
// if FIRAuth.auth()?.currentUser?.uid == nil {
if FIRAuth.auth() == nil {

self.logInOutlet.hidden = false
self.logOutOutlet.hidden = true
self.bookingsOutlet.hidden = true

// it still prints the user id, why?
if let userNOTLogged = FIRAuth.auth()?.currentUser?.uid {
print("User should NOT be logged \(userNOTLogged)")


// if the user is logged in
// hide: logIn Button
// show: logOut button & bookings button
} else if

FIRAuth.auth() != nil {
self.logInOutlet.hidden = true
self.logOutOutlet.hidden = false
self.bookingsOutlet.hidden = false

// Log in code

FIRAuth.auth()?.signInWithEmail(email.text!, password: password.text!, completion: { (authData, error) in

let customError = error?.localizedDescription
if error != nil {

// display an alert with the error

} else {
print("The user has been logged in")

//if signIn was successful, instantiate the view controller with identifier SWrevelViewidentifier
let toMenuOptions = self.storyboard?.instantiateViewControllerWithIdentifier("SWrevelViewidentifier")
self.presentViewController(toMenuOptions!, animated: true, completion: nil)



I had this exact same problem. Since this happens when you relaunch, try putting this code in your app.delegate.

FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
  if let user = user {
    // User is signed in.
  } else {
    // No user is signed in.