gpt4 book ai didi

ios - Swift 3 Firebase : current user login success, 事件已在 Firebase 控制台中删除此用户

转载 作者:搜寻专家 更新时间:2023-10-31 22:58:36 26 4
gpt4 key购买 nike

在登录表单中通过代码成功创建 Firebase 用户后。我在 Firebase 控制台中删除了这个新用户,退出了应用程序。再次打开应用程序时,它仍然提示登录成功,用户已被管理员删除。这是为什么,如何在登录表单中知道用户已删除?

引用我下面的登录表单代码:

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

最佳答案

因为用户仍然处于登录状态。第一步是注销,您可以在 applicationDidEnterBackground 函数内的 appDelegate 中进行。只需这样调用:
尝试! 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.")
}

}

}

关于ios - Swift 3 Firebase : current user login success, 事件已在 Firebase 控制台中删除此用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40759538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com