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

Jon*_*Cox 34 ios firebase swift

使用以下代码对用户进行身份验证后(下面是我的代码的修剪版本,因此只显示成功的登录逻辑)...

let firebaseReference = Firebase(url: "https://MY-FIREBASE.firebaseio.com")
Run Code Online (Sandbox Code Playgroud)

 

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)")
                    }
            })
        }
    })
}
Run Code Online (Sandbox Code Playgroud)

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

如果您随后删除了应用程序并在同一设备上重新安装,则此调用(我在应用程序委托中使用以确定用户是否已登录)将始终返回他们已登录的状态.

if firebaseReference.authData == nil {
    // Not logged in
} else {
    // Logged in
}
Run Code Online (Sandbox Code Playgroud)

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

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

Rob*_*rco 42

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

如果您要手动清除数据,可以将一些其他元数据与应用程序一起存储,然后手动调用FirebaseRef.unauth()以清除持久会话.请参阅#4747404:卸载应用程序时删除钥匙串项以获取其他参考.

  • 谢谢罗布。需要补充一点的是,根据 FirebaseAuth 3.1.1,您现在需要调用 `try! FIRAuth.auth()!.signOut()` (2认同)

Dar*_*ari 21

在AppDelegate的didFinishLaunchingWithOptions函数(返回true之前)末尾添加以下代码可以快速完成.

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
}
Run Code Online (Sandbox Code Playgroud)


小智 7

对于快速4相同的答案:

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
}
Run Code Online (Sandbox Code Playgroud)


Sur*_*mar 5

使用以下扩展名:

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

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

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