如何在 Firebase 中创建不同的用户组? [英] How to create different user groups in Firebase?

查看:31
本文介绍了如何在 Firebase 中创建不同的用户组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xcode 制作销售点应用.

我希望这款应用能够跟踪库存商品,允许用户使用条形码搜索库存、扫描库存、添加或查找库存.库存商品将存储在用户的商店"中.

我想要三种类型的用户:

  1. 经理用户,创建商店并拥有商店的完全访问权限.他可以管理所有员工用户",例如删除他们的访问权限,员工用户必须由他使用唯一代码邀请才能加入他的商店.他将拥有员工用户"所没有的特定能力,例如添加和编辑库存的能力.
    经理帐户的样子

  2. 员工用户,有限制.(例如,看不到编辑用户页面,也看不到编辑经理用户拥有的股票的选项).
    员工帐户的样子

  3. 购物者用户,他只能看到商店和商店中的商品.购物者用户帐户的样子

附加的 UI 设计应该能说明我的意思.如您所见,经理用户有一个管理员工选项卡,我希望只有他可以拥有而其他用户没有.在查看库存时,经理还可以选择了解更多详细信息,而员工用户则没有.购物者帐户只能访问两个功能,查找商店和检查商店中的库存.

所以我的问题是,如何在 Firebase 中为我的应用编码三种不同类型的用户?&如何仅向他们显示我想向他们显示的选项,同时向其他用户显示其他选项?

解决方案

这是一个非常广泛的主题,其中很多取决于您如何实现应用程序的各个部分.以下是众多可能的答案之一,只是为了让您开始使用一些链接.

如果您的应用将其数据存储在 Firebase 数据库产品之一(实时数据库,或Cloud Firestore),或者是否将文件存储在 Cloud Storage 通过 Firebase,那么您可能希望将每个用户的角色作为自定义声明存储在该用户的个人资料中.

Firebase 身份验证文档说明了如何设置此类自定义声明,例如如何从 Node.js 脚本将用户的 admin 属性设置为 true:

<块引用>

admin.auth().getUserByEmail('user@admin.example.com').then((user) => {//确认用户已验证.如果(user.emailVerified){//添加额外权限的自定义声明.//这将由用户在令牌刷新或下次登录新设备时获取.返回 admin.auth().setCustomUserClaims(user.uid, {管理员:真实});}}).catch((错误) => {控制台日志(错误);});

同样,您可以在 role 属性中设置您的用户角色.然后可以查看实时数据库的服务器端安全规则,Cloud Firestore 或 Cloud Storage 如果用户的角色允许他们访问他们尝试访问的特定数据.

在客户端代码中,您可以解码用户的令牌以访问相同的声明并为它们优化用户界面

I am making a point of sale app, using Xcode.

I want this app to keep track of stock items, allow users to search stock, scan stock, add or lookup stock using a barcode. Items of stock will be stored in a users 'store'.

I want to have three types of users:

  1. A manager user, who creates a store and has full access to the store. He can manage all of the 'employee users', such as deleting their access and employee users must be invited by him to join his store using a unique code. He will have specific abilities that 'employee users' do not such as the ability to add and edit stock.
    What a manager account would look like

  2. A employee user, who has limitations. (e.g. cannot see edit users page and not option to edit stock that manager user has).
    What a Employee account would look like

  3. A shopper user who can only see a store and the items in a store. What a shopper users account would look like

The attached UI design should show what I mean. As you can see, the manager user has a manage employee tab that I wish only he can have and the other users do not. The manager has also got the option for more details when looking at stock, while the employee user does not. The shopper account has only got access to two functions, looking up a store, and checking the stock in a store.

So my question is, How do I code three different types of users in Firebase for my app? & How do I show them only options that I want to show them while showing other options to other users?

解决方案

This is an incredibly broad topic and lots of it depends on how you implement the various parts of your app. Below is one of the many possible answers, just in an effort to get you started with some links.

If your app stores its data in one of Firebase database offerings (Realtime Database, or Cloud Firestore), or if it stores files in Cloud Storage through Firebase, then you'll likely want to store the role of each user as a custom claim in the profile of that user.

The Firebase Authentication documentation shows how to set such custom claims, for example how to set the admin property of a user to true from a Node.js script:

admin.auth().getUserByEmail('user@admin.example.com').then((user) => {
  // Confirm user is verified.
  if (user.emailVerified) {
    // Add custom claims for additional privileges.
    // This will be picked up by the user on token refresh or next sign in on new device.
    return admin.auth().setCustomUserClaims(user.uid, {
      admin: true
    });
  }
}).catch((error) => {
  console.log(error);
});

Similarly you could set your user roles in a role property. Then you can check in the server-side security rules of the Realtime Database, Cloud Firestore, or Cloud Storage if the user has a role that allows them access to the specific data they're trying to access.

In the client-side code you can then decode the user's token to get access to the same claims and optimize the UI for them

这篇关于如何在 Firebase 中创建不同的用户组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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