Firebase - 删除和重新安装应用程序不会取消对用户的身份验证

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

通过以下代码对用户进行身份验证后(以下是我的代码的精简版,因此仅显示成功登录的逻辑)...

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)")
                    }
            })
        }
    })
}

(即启动并 运行 应用程序,成功登录)。

如果您随后删除该应用程序并将其重新安装到同一台设备上,则此调用(我在应用程序委托中使用它来确定用户是否已登录)将始终 return 他们已登录英寸

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

这是为什么?我本以为删除应用程序并重新安装它应该清除所有数据。

如果您在 iOS 模拟器中重置内容和设置,并安装应用程序,firebaseReference.authData 属性 将再次成为 nil

Firebase 身份验证会话保存在用户设备上的 iOS 钥匙串中。应用程序的钥匙串数据在卸载应用程序时不会删除。

如果您希望手动清除数据,您可以将一些额外的元数据与您的应用程序一起存储,然后手动调用 FirebaseRef.unauth() 以清除持久会话。有关其他参考,请参阅 #4747404: Delete keychain items when an app is uninstalled

在 AppDelegate 的 didFinishLaunchingWithOptions 函数末尾(在 return true 之前)添加以下代码可以快速运行。

Swifty 方式

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
}

对于swift4个相同的答案:

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
}

使用以下扩展名:

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

并在 Appdelegate 的“...didFinishLaunchingWithOptions...”方法中调用它:

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