通过登录表单中的代码成功创建Firebase用户。我在Firebase控制台中删除了这个新用户,退出了应用。再次打开应用程序时,它仍会通过admin已删除的用户通知登录成功。为什么这样,以及如何在登录表单中知道用户已经删除了?
请参阅下面的登录表格代码:
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.")
}
}
}
的示例代码
答案 0 :(得分:1)
因为用户仍在登录。
第一步是注销,你可以在appDlegate里面的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()
}
第二步是进行新的登录(如果用户允许在... userDefault ....中保存电子邮件和密码),并获取有关applicationDidBecomeActive函数内用户登录的新信息。
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.")
}
}
}