Firebase - Deleting and reinstalling app does not un-authenticate a user

IosSwiftFirebase

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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
}

Solution 3 - Ios

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
}

Solution 4 - Ios

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
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJon CoxView Question on Stackoverflow
Solution 1 - IosRob DiMarcoView Answer on Stackoverflow
Solution 2 - IosBibekView Answer on Stackoverflow
Solution 3 - IosShubham TomarView Answer on Stackoverflow
Solution 4 - IosSurendra KumarView Answer on Stackoverflow