Firebase:设置其他用户属性

Firebase: setting additional user properties

我想向 Firebase 用户对象添加 属性。 user documentation 表示我只能使用 Firebase 实时数据库存储其他属性。

我不确定这在实践中如何工作

以下在实践中是什么意思?

You cannot add other properties to the Firebase User object directly; instead, you can store the additional properties in your Firebase Realtime Database.

我解读如下:

"you cannot modify properties of a FIRUser object but you can combine this with additional objects"

我找到了 set 函数 documentation,我是这样插入的:

  var userRef = ref.child("users");
  userRef.set({
    newfield: "value"
  });

这是明智的做法吗?

你快到了。在遗留的 Firebase 文档中,我们有一个关于 storing such additional user data.

的部分

关键是在用户的uid下存储附加信息:

    let newUser = [
        "provider": authData.provider,
        "displayName": authData.providerData["displayName"] as? NSString as? String
    ]
    // Create a child path with a key set to the uid underneath the "users" node
    // This creates a URL path like the following:
    //  - https://<YOUR-FIREBASE-APP>.firebaseio.com/users/<uid>
    ref.childByAppendingPath("users")
       .childByAppendingPath(authData.uid).setValue(newUser)

我添加了一条注释,我们也应该在新文档中添加此信息。我们只需要为它找个好地方。

根据 Custom Claims 文档,

The Firebase Admin SDK supports defining custom attributes on user accounts. [...] User roles can be defined for the following common cases:

  • Add an additional identifier on a user. For example, a Firebase user could map to a different UID in another system.

[...] Custom claims payload must not exceed 1000 bytes.

但是,根据 Best Practices:

,仅对与身份验证相关的用户数据执行此操作,而不是对一般个人资料信息执行此操作

Custom claims are only used to provide access control. They are not designed to store additional data (such as profile and other custom data). While this may seem like a convenient mechanism to do so, it is strongly discouraged as these claims are stored in the ID token and could cause performance issues because all authenticated requests always contain a Firebase ID token corresponding to the signed in user.

Use custom claims to store data for controlling user access only. All other data should be stored separately via the real-time database or other server side storage.