从身份验证数据库中删除匿名用户 [英] Remove anonymous user from Auth database

查看:78
本文介绍了从身份验证数据库中删除匿名用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Firebase 数据库,对下一种情况几乎不感到困惑.

I'm using Firebase database and little confused with next situation.

假设我有待办事项.我使用标准Firebase Auth系统在用户设备之间进行同步items.但是在其他情况下,用户可以匿名工作而无需同步.

Let's imagine i have todo app. I use standard Firebase Auth system for sync items between user devices. But in other case user could work anonymously without synchronization.

第1步:

第一次启动用户应用程序,并在AppDelegate中创建了匿名用户:

User launch app first time, and in AppDelegate i created Anonymous user:

FIRAuth.auth()?.addStateDidChangeListener { auth, user in
  if let _user = user {
    if _user.isAnonymous {
        print("User logged in as anonymous. Saving uid to user defaults storage.")
        UserDefaults.standard.setValue(_user.uid, forKey: "uid")
    } else {
         print("User logged in with email: \(_user.email)")
    }
  } else {
     FIRAuth.auth()?.signInAnonymously() { (user, error) in
        if let _error = error {
           print("Anonymous signIn error: \(_error)")
        }
     }
  }
}

第2步:

此匿名用户创建了一些待办事项并决定注册:

This anonymous user created few todo items and decided signUp:

let credential = FIREmailPasswordAuthProvider.credential(withEmail: emailField.text!, password: passwordField.text!)
FIRAuth.auth()?.currentUser?.link(with: credential, completion: { user, error in
    if error == nil {
        FIRAuth.auth()!.signIn(withEmail: emailField.text!,
                                       password: passwordField.text!)
     }
 })

所以我修改了以前的代码,并更改了创建的待办事项的所有者:

So i have modified previous code and change owner of created todo items:

if let prevUserUID = UserDefaults.standard.string(forKey: "uid"), prevUserUID != _user.uid {
    FIRDatabase.database().reference().child("todo-items").queryOrdered(byChild: "user").queryEqual(toValue: prevUserUID).observe(.value, with: { snapshot in
    for item in snapshot.children {
        var todoItem = TodoItem(snapshot: item as! FIRDataSnapshot)
        todoItem.user = _user.uid
        todoItem.ref?.setValue(todoItem.toAnyObject())
    }
  })
  print("Data migrated.")
}

确定一切正常.但是,现在用户已注销.我创建了一个新的匿名用户.然后他再次登录,我合并了数据.但是我无法链接帐户,因为我之前已经做过.而且我无法从Auth数据库中删除匿名用户(我的问题).然后我们得到僵尸!未使用的匿名帐户.如果用户登录/注销1000次,我们将获得1000个匿名帐户.

Ok everything works. But, now user logged out. An i created new anonymous user. Then he login again and i merge data. But i can't link account, because i already did it before. And i can't delete anonymous user from Auth database(my question). And we get zombie! unused anonymous account. And if user will log in/log out 1000 times we get 1000 anonymous accounts.

推荐答案

当您将凭据与currentUser链接时,就已经登录了,不需要:

When you link the credential with the currentUser you are logged already, don't need for:

FIRAuth.auth()!.signIn

您保留匿名用户的UID.

You keep the UID of the anonymous user.

如果用户已经有一个帐户,在这种情况下,您需要:

If user already have an account, in this case you need to:

currentUser.delete()

然后使用提供的凭据登录

And then logIn with the credentials provided

这篇关于从身份验证数据库中删除匿名用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆