简体   繁体   English

Firebase:设置其他用户属性

[英]Firebase: setting additional user properties

I'd like to add a property to a Firebase user object.我想向 Firebase 用户对象添加一个属性。 The user documentation says that I can only store additional properties using the Firebase real time database.用户文档说我只能使用 Firebase 实时数据库存储其他属性。

I am unsure on how this can works in practice.我不确定在实践中如何运作

What does the following mean in practice?以下在实践中意味着什么?

You cannot add other properties to the Firebase User object directly;您不能直接向 Firebase User 对象添加其他属性; instead, you can store the additional properties in your Firebase Realtime Database.相反,您可以将其他属性存储在 Firebase 实时数据库中。

I interpret it as following:我解释如下:

"you cannot modify properties of a FIRUser object but you can combine this with additional objects" “您不能修改FIRUser对象的属性,但可以将其与其他对象结合使用”

I found the set function documentation which I interpet in this way:我找到了我以这种方式解释的set函数文档

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

Is this a sensible approach?这是一种明智的做法吗?

You're almost there.你快到了。 In the legacy Firebase documentation, we had a section on storing such additional user data .在旧版 Firebase 文档中,我们有一节介绍了存储此类额外的用户数据

The key is to store the additional information under the user's uid :关键是将附加信息存储在用户的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)

I've added a note that we should add this information in the new documentation too.我已经添加了一个注释,我们也应该在新文档中添加这些信息。 We just need to find a good spot for it.我们只需要为它找到一个好地方。

According to the Custom Claims documentation,根据海关索赔文件,

The Firebase Admin SDK supports defining custom attributes on user accounts. Firebase Admin SDK 支持在用户帐户上定义自定义属性。 [...] 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.例如,Firebase 用户可以映射到另一个系统中的不同 UID。

[...] Custom claims payload must not exceed 1000 bytes. [...] 自定义声明负载不得超过 1000 字节。

However, do this only for authentication-related user data, not for general profile information, per the 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.虽然这似乎是一种方便的机制,但强烈建议不要这样做,因为这些声明存储在 ID 令牌中并且可能导致性能问题,因为所有经过身份验证的请求始终包含与登录用户对应的 Firebase ID 令牌。

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.所有其他数据应通过实时数据库或其他服务器端存储单独存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有表单的其他用户信息Firebase网站 - Additional user info firebase web with a form Firebase-将其他用户数据添加到单独的数据库 - Firebase - Adding additional user data to separate DB 在React中从Firebase获取用户对象属性 - Get user object properties from Firebase in React 将 Firebase 用户 photoURL 值设置为 null - Setting a Firebase User photoURL value to null 如何使用 Firebase 9 email 和密码创建用户并将该用户的附加数据保存在 firebase 数据库集合中? - How to create users with Firebase 9 email and password and save that user's additional data in firebase db collection? 在实例化时绑定附加属性 - Bind additional properties at instantiation 错误:用户更改日期时设置jQuery FullCalendar属性 - Error: setting jQuery FullCalendar properties when the user changes the date 在firebase实时数据库中安全地设置新用户的组字段 - Setting group field of new user safely in firebase realtime db @firebase/database:FIREBASE 警告:用户回调引发异常。 TypeError:无法读取未定义的属性(读取“AccountStatus”) - @firebase/database: FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot read properties of undefined (reading 'AccountStatus') 木材框架中元素的其他属性 - Additional properties in element in the timber framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM