首页 文章

使用Firebase用户UID获取相关数据库条目的密钥 - 好或坏的做法?

提问于
浏览
4

使用用户的UID(由用户通过Firebase Auth创建时由Firebase生成)作为关联数据库条目的关键字是否有任何问题?

例如,这里:

/config/user.uid/configObject

...每个用户都有一个配置条目 . 由于Firebase 's database rules (where settings on a parent node apply to all child nodes) it'经常需要创建单独的配置树(在示例之后,需要对 /users 树进行读写保护) .

我可以看到这可能是不好的做法的唯一原因是,如果用户的UID发生了变化...... Firebase是否保证永远不会发生这种情况?我缺少什么?

1 回答

  • 3

    特定用户的UID永远不会改变(尽管他们做了change the format a few months ago;旧格式包括提供者) .

    这是一个很好的做法,在许多情况下是必要的,并且可能在某个地方推荐 . 您需要将用户信息非规范化为多个节点,以获得更好的性能和授权,如您所述 . 它可能在伪代码/规则中看起来像这样:

    - users_private (.read: $uid == auth.uid)
      - $uid
        - email
    
    - users_public (.read: true)
      - $uid
        - name
        - photo
    
    - users_roles (.read: dependent on some other rules)
      - $uid
        - is_admin
        - is_editor
    

相关问题