swift - 如何使用 Swift 和 Firebase 使用 UIAlert 删除当前用户
问题描述
快速提问 我试图让用户选择在 Xcode 上使用 swift 从 firebase 中删除他们的帐户。
我目前有一个“删除”按钮,它会导致一个 UIAlert 询问用户是否确定他们想要像这样删除他们的帐户(以防万一)
我执行此操作的代码如下
//Handles delete current user account (not fnished yet)
@IBAction func deleteAccount(_ sender: Any) {
createAlert2(title: "Delete Account", message: "Are you sure you want to delete your account? This will permanently erase your account.")
}
到这个函数
func createAlert2 (title:String, message:String){
let alert2 = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert2.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { (action) in
alert2.dismiss(animated: true, completion: nil)
}))
alert2.addAction(UIAlertAction(title: "Delete", style: UIAlertActionStyle.destructive, handler: { (action) in
let user = Auth.auth().currentUser
user?.delete { error in
if error != nil {
// An error happened.
} else {
// Account deleted.
print("user deleted")
}
}
let controller2 = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController
self.present(controller2, animated: true, completion: nil)
}))
self.present(alert2, animated: true, completion: nil)
我的目标是删除用户并将他们发送回那个 MenuViewController。但它似乎所做的只是将用户发送到该菜单视图,而不是实际删除他们在 firebase 上的帐户。感谢任何帮助,我总是喜欢并勾选答案。谢谢:)
解决方案
如果您打印错误或只是在 user?.delete 之后放置几个断点,您很可能会看到如下内容:
可选 - 一些:错误域 = FIRAuthErrorDomain 代码 = 17014 “此操作很敏感,需要最近的身份验证。在重试此请求之前再次登录。” UserInfo={NSLocalizedDescription=此操作很敏感,需要最近的身份验证。在重试此请求之前再次登录。error_name=ERROR_REQUIRES_RECENT_LOGIN}
这意味着您需要使用凭据(电子邮件、密码)重新注册或重新验证才能删除您的用户。您可以使用以下代码进行一些改进(选择获取凭据的方式)
// 最新的 firebase + swift 4
let user = Auth.auth().currentUser
let password = "mypassword from stream - ask for a password from your user"
let email = user?.email
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
user?.reauthenticateAndRetrieveData(with: credential, completion: { (result, error) in
if let error = error {
// handle error here
print(error)
}else{
user?.delete { error in
if let error = error {
// handle your error here
print(error)
}else{
do {
// handle your signout smth like:
try Auth.auth().signOut()
self.dismiss(animated: true, completion: nil)
} catch let logoutError {
// handle your error here
print(logoutError)
}
}
}
}
})
不幸的是,这个问题文档很差(尤其是最新版本)。希望你觉得我的回答有用。注意***:让您的用户能够直接删除他的个人资料,这是一个非常糟糕的主意(恕我直言),如果您发送电子邮件通知并禁用用户并使他的令牌或 UID 无效,则更好,它将在未来为您提供更大的灵活性。
推荐阅读
- php - 使用 PHP 和回显数据循环进入多维数组
- coq - Coq:证明归纳关系(vs Fixpoint)
- firebase - 那么谷歌的伦敦数据中心到底在哪里呢?
- c# - 带有 EF Core 的 Blazor WASM:将数据模型代码放在哪里?
- cypress - Cypress:获取每个元素的属性值
- c# - C# 2D 数组匹配并返回 1 维
- ios - 错误域=NSItemProviderErrorDomain 代码=-1000“无法加载 public.jpeg 类型的表示”
- ruby - Ruby 程序计算该值在散列中的所有值中出现的次数
- .net - 在 azure 代理中运行 nunit 测试时找不到 testhost.deps.json 文件
- python - TurboGears:管理 crud 中的 sqlalchemy.exc.ArgumentError