简体   繁体   English

为无服务器 firebase 应用程序的用户分配角色

[英]Assign roles to users for serverless firebase app

I have a serverless application in React that uses a firebase firestore.我在 React 中有一个使用 firebase 火库的无服务器应用程序。

The app creates new users programmatically and data to a doc with name the same as the newly created user id.该应用程序以编程方式创建新用户,并将数据添加到名称与新创建的用户 ID 相同的文档中。

firebase.auth().createUserWithEmailAndPassword(
        customerEmail.current.value, 
        customerPassword
    ).then(function(user) {
        console.log("UID!!!!!!!!!!!!!!!!!!!!!!!", user, user.user.uid)
        firebase
        .firestore()
        .collection('sessions')
        .doc(user.user.uid).set(
            {...context.state}
        ).then(() => {
            console.log("DATA SAVED SUCCESSFULLY!")
        })
    })

Next I want to assign roles as I create these users.接下来,我想在创建这些用户时分配角色。

I really want to use the admin sdk to assign custom claims to these users - is there a way to do this while remaining serverless?我真的很想使用管理员 sdk 为这些用户分配自定义声明 - 有没有办法在保持无服务器的同时做到这一点?

If not, what are the alternatives?如果没有,有什么替代方案?

You indeed need to use the Admin SDK for that.您确实需要为此使用 Admin SDK。

As explained in the dedicated section of the doc , “Custom claims should be set from a privileged server environment by the Firebase Admin SDK ”.文档的专用部分所述,“自定义声明应由Firebase 管理员 SDK从特权服务器环境设置”。

With “privileged server environment” the doc means a server that you fully control or a Cloud Function in your Firebase Project that runs back-end code (and that you also control, since it is in your own Project).对于“特权服务器环境”,文档表示您完全控制的服务器或Firebase 项目中运行后端代码的云 Function (并且您也可以控制,因为它在您自己的项目中)。

Cloud Functions are "a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features". Cloud Functions是“一个无服务器框架,可让您自动运行后端代码以响应由 Firebase 功能触发的事件”。

For example with the Admin Node.js SDK, you just have to call the setCustomUserClaims() method, passing the uid of the user and an object defining the claims to be set.例如,对于管理员 Node.js SDK,您只需调用setCustomUserClaims()方法,传递用户的uid和 object 来定义声明。

In your case, you should most probably trigger a Cloud Function upon user creation, see the Cloud Functions corresponding doc .在您的情况下,您很可能应该在创建用户时触发 Cloud Function,请参阅 Cloud Functions 相应的文档


You don't give a lot of details on which kind of roles you want to assign.对于要分配的角色类型,您没有提供很多详细信息。 If there are different possible roles, you will need a way to decide which role(s) to assign to which user.如果有不同的可能角色,您将需要一种方法来决定将哪些角色分配给哪个用户。

You may be interested by the following article I wrote on "How to create an Admin module for managing users access and roles"您可能会对我在“如何创建管理模块以管理用户访问权限和角色”中撰写的以下文章感兴趣

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM