Swift 3 Firebase:当前用户login成功,事件已经在Firebase控制台中删除此用户

在login表单中通过代码创buildFirebase用户之后。 我在Firebase控制台中删除了这个新用户,退出了该应用。 当再次打开应用程序时,它仍然通知pipe理员已经删除的用户login成功。 为什么会这样,以及如何在login表单中知道用户已经删除了?

请参阅我的下面的login表单代码:

override func viewDidLoad() { super.viewDidLoad() // get the current user is by setting a listener on the Auth object: FIRAuth.auth()!.addStateDidChangeListener() { auth, user in if user != nil { // User is signed in. print("start login success: " + (user?.email)! ) //self.performSegue(withIdentifier: loginToList, sender: nil) } else { // No user is signed in. print("No user is signed in.") } } //get the currently signed-in user by using the currentUser property. If a user isn't signed in, currentUser is nil: if let curUser = FIRAuth.auth()?.currentUser { // User is signed in. print("start current user: " + curUser.email! ) } else { // No current user is signed in. print("Currently, no user is signed in.") } } } 

来自https://firebase.google.com/docs/auth/ios/manage-users的示例代码

因为用户仍然login。 第一步是注销,你可以在appDelegate里面的applicationDidEnterBackground函数中做到这一点。 只需调用这个:
try! FIRAuth.auth()!.signOut()

  func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. try! FIRAuth.auth()!.signOut() } 

第二步是做一个新的login(如果用户允许保存电子邮件和密码在… userDefault ….例如),并得到有关用户在applicationDidBecomeActive函数内login的新信息。

 func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. // Check if the User data exist let User = UserDefaults.standard guard let myEmail = User.object(forKey: "userName") as? String else { print("No User") return } //Check if the user wants the automatic access guard User.object(forKey: "automaticLogIn") as! Bool else { print("Automatic LogIn disable") return } // Now you can to do the automatic login let password = User.object(forKey: "password") as! String FIRAuth.auth()?.signIn(withEmail: myEmail, password: password, completion: { (user, error) in if error == nil{ print("logIn succesfull") }else{ let typeError = FIRAuthErrorCode(rawValue: error!._code)! switch typeError { case .errorCodeUserNotFound: print("user not found") case .errorCodeWrongPassword: print("wrong password") default: break } } }) } 

现在你可以调用FIRAuth.auth()!.addStateDidChangeListener() ,在你想要的地方,例如在viewDidLoad

 override func viewDidLoad() { super.viewDidLoad() // Save the user data inside userDefault, you can do it where you want.... like a form inside an alertView let User = UserDefaults.standard User.set("userEmail...", forKey: "userName") User.set("UserPassword", forKey: "password") User.set(true, forKey: "automaticLogIn") FIRAuth.auth()!.addStateDidChangeListener() { auth, user in if user != nil { // User is signed in. print("start login success: " + (user?.email)! ) //self.performSegue(withIdentifier: loginToList, sender: nil) } else { // No user is signed in. print("No user is signed in.") } } }