要从身份验证标签中删除用户,我使用以下代码:
if let user = FIRAuth.auth()?.currentUser{
user.delete(completion: nil)
}
当用户使用电子邮件/密码组合注册时,这种情况很正常,但是当他们注册社交帐户时,他们不会被删除,我仍然可以在“身份验证”选项卡中看到他们的电子邮件,UID等。
如何从Firebase中删除这些用户?
提前致谢
答案 0 :(得分:1)
要从firebase删除google和facebook用户,您必须重新验证用户。删除帐户需要用户最近登录。如果您执行其中一项操作,并且用户很久以前已登录,则操作将失败并显示FIRAuthErrorCodeCredentialTooOld
错误。发生这种情况时,请通过从用户获取新的登录凭据并将凭据传递到reauthenticate
来重新验证用户身份。
let user = Auth.auth().currentUser
var credential: AuthCredential
// Prompt the user to re-provide their sign-in credentials
user?.reauthenticate(with: credential) { error in
if let error = error {
// An error happened.
} else {
// User re-authenticated.
}
}
现在,您可以使用deleteWithCompletion
方法删除用户帐户。
let user = Auth.auth().currentUser
user?.delete { error in
if let error = error {
// An error happened.
} else {
// Account deleted.
}
}
答案 1 :(得分:0)
谢谢Jogendra,重新认证是解决问题的关键,我接受了答案。这是使用Swift 3的完整答案,其中包括用于检索用户凭据的代码。
func deleteAllUserData(){
if let user = FIRAuth.auth()?.currentUser{
user.delete(completion: { (err) in
if(err != nil){
print(err!.localizedDescription)
//Try to re-authenticate if there is an error
self.getProvider()
}else{
self.deleteData()
}
})
}
}
//There are different methods to retrieve the credentials based on the auth type so you first need to know which type of authentication is being used
func getProvider(){
if let providerData = FIRAuth.auth()?.currentUser?.providerData {
for userInfo in providerData {
switch userInfo.providerID {
case "facebook.com":
if let credential = facebookCredential(){
self.reauthenticate(credential: credential)
}
case "google.com":
if let credential = googleCredential(){
self.reauthenticate(credential: credential)
}
print("user is signed in with google")
case "password":
let alert = UIAlertController(title: "Sign In", message: "Please Sign In again to your Locket account to confirm that you want to delete all your data", preferredStyle: .alert)
alert.addTextField { (textField: UITextField) in
textField.placeholder = "Email"
}
alert.addTextField { (textField: UITextField) in
textField.placeholder = "Password"
textField.isSecureTextEntry = true
}
let noAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let yesAction = UIAlertAction(title: "OK", style: .destructive, handler: { (action:UIAlertAction) in
let emailTextField = alert.textFields![0]
let passwordTextField = alert.textFields![1]
if let credential = self.emailCredential(email: emailTextField.text!, password: passwordTextField.text!){
self.reauthenticate(credential: credential)
}else{
print("error")
}
})
alert.addAction(yesAction)
alert.addAction(noAction)
self.present(alert, animated: true, completion: nil)
default:
print("unknown auth provider")
}
}
}
}
func reauthenticate(credential:FIRAuthCredential){
FIRAuth.auth()?.currentUser?.reauthenticate(with: credential) { error in
if let error = error {
print("reauth error \(error.localizedDescription)")
} else {
print("no reauth error")
self.deleteData()
}
}
}
func facebookCredential() -> FIRAuthCredential? {
let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
return credential
}
func emailCredential(email:String,password:String) -> FIRAuthCredential? {
let credential = FIREmailPasswordAuthProvider.credential(withEmail: email, password: password)
return credential
}
func googleCredential() -> FIRAuthCredential? {
guard let user = GIDSignIn.sharedInstance().currentUser else {return nil}
guard let authentication = user.authentication else {return nil}
let credential = FIRGoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
return credential
}
func deleteData(){
// Delete all other data related to user
}