64

After authenticating a user with the following code (below is a trimmed version of my code, so only the successful login logic is shown)...

let firebaseReference = Firebase(url: "https://MY-FIREBASE.firebaseio.com")

 

FBSession.openActiveSessionWithReadPermissions(["public_profile", "user_friends"], allowLoginUI: true,
    completionHandler: { session, state, error in

        if state == FBSessionState.Open {
            let accessToken = session.accessTokenData.accessToken
            firebaseReference.authWithOAuthProvider("facebook", token: accessToken,
                withCompletionBlock: { error, authData in

                    if error != nil {
                        // Login failed.
                    } else {
                        // Logged in!
                        println("Logged in! \(authData)")
                    }
            })
        }
    })
}

(I.e. Launching and running the app, logging in successfully).

If you then delete the app and reinstall it on the same device, this call - which I am using in the app delegate to determine if a user is logged in - will always return that they are logged in.

if firebaseReference.authData == nil {
    // Not logged in
} else {
    // Logged in
}

Why is that? I would have thought deleting the app and reinstalling it should wipe all data.

If you reset the Content and Settings in the iOS simulator, and the install the app, the firebaseReference.authData property will once again be nil.

Jon Cox
  • 10,622
  • 22
  • 78
  • 123
  • I think the session might be kept in a Safari cookie. Can you wipe the cookies of Safari to see if that's true. It would still not solve your problem of course, but at least help identify a cause. – Frank van Puffelen Jan 12 '15 at 02:18

4 Answers4

62

The Firebase authentication session is persisted on the user's device in the iOS keychain. The keychain data for the application is not removed when the application is uninstalled.

If you're looking to manually clear the data, you can store some additional metadata along with your application and manually call FirebaseRef.unauth() to clear the persisted session. See #4747404: Delete keychain items when an app is uninstalled for an additional reference.

Community
  • 1
  • 1
Rob DiMarco
  • 13,226
  • 1
  • 43
  • 55
  • 1
    Perfect! That clears everything up. A quick use of NSUserDefaults will do the trick. Thank you. – Jon Cox Jan 12 '15 at 10:01
  • 2
    Thanks Rob. Just a point to add, as per FirebaseAuth 3.1.1, you will now need to call `try! FIRAuth.auth()!.signOut()` – Harry Bloom Apr 19 '17 at 11:11
26

Adding below code at the end of didFinishLaunchingWithOptions function (before return true) of AppDelegate works swiftly.

Swifty way

let userDefaults = UserDefaults.standard
if userDefaults.value(forKey: "appFirstTimeOpend") == nil {
    //if app is first time opened then it will be nil
    userDefaults.setValue(true, forKey: "appFirstTimeOpend")
    // signOut from Auth
    do {
        try Auth.auth().signOut()
    }catch {
        
    }
    // go to beginning of app
} else {
    //go to where you want
}

Swift 3.*

let userDefaults = NSUserDefaults.standardUserDefaults()
if userDefaults.valueForKey("appFirstTimeOpend") == nil {
    //if app is first time opened then it will be nil
    userDefaults.setValue(true, forKey: "appFirstTimeOpend")
    // signOut from FIRAuth
    do {
        try FIRAuth.auth()?.signOut()
    }catch {
    
    }
    // go to beginning of app
} else {
   //go to where you want
}
Bibek
  • 3,689
  • 3
  • 19
  • 28
  • The problem I see with this solution though is that it will also log out every customer that is already logged in and gets the update of the app (in which you will include this code). So if you haven't included this code from the first release of your app you will force everyone to log out and log in again. Am I missing something? – Alexandros Trepeklis Dec 21 '22 at 12:39
13

For swift 4 the same Answer:

let userDefaults = UserDefaults.standard
if userDefaults.value(forKey: "appFirstTimeOpend") == nil {
    //if app is first time opened then it will be nil
    userDefaults.setValue(true, forKey: "appFirstTimeOpend")
    // signOut from FIRAuth
    do {
        try Auth.auth().signOut()
    }catch {

    }
    // go to beginning of app
} else {
    //go to where you want
}
Chris
  • 1,475
  • 1
  • 20
  • 35
Shubham Tomar
  • 293
  • 4
  • 13
5

Use below extension :

extension AppDelegate{
func signOutOldUser(){
    if let _ = UserDefaults.standard.value(forKey: "isNewuser"){}else{
        do{
            UserDefaults.standard.set(true, forKey: "isNewuser")
            try Auth.auth().signOut()
        }catch{}
    }
} 
}

and call this in '...didFinishLaunchingWithOptions...' method of Appdelegate:

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    signOutOldUser()
    return true
}
Surendra Kumar
  • 647
  • 7
  • 13