没有 Cloud Functions 的 Firebase 自定义声明 [英] Firebase Custom Claims without Cloud Functions

查看:23
本文介绍了没有 Cloud Functions 的 Firebase 自定义声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种无需通过云功能即可创建自定义声明的方法?

Is there a way to create a custom claim without having to go through cloud functions?

我真的只需要为几个帐户设置它,而我目前的计划是spark.我现在还不能升级.但是我需要测试这个功能来实现基于角色的身份验证.

I really only need to set it up for a few accounts, and my current plan is spark. I can't upgrade just yet. But I need to test this feature to implement role based authentication.

谢谢,请尽可能详细,因为我是 firebase cli 的新手.

Thank you and please be as detailed as possible as I'm new to firebase cli.

推荐答案

是的,如果您只需要设置几个用户,那么您可以在本地运行脚本来添加声明.只需按照以下步骤操作:

Yes, if you just need to setup a few users then you run a script locally to add the claims. Just follow the steps:

1.创建 NodeJS 项目

npm init -y

//Add Firebase Admin
npm install firebase-admin

2.将此代码写入 JS 文件 (addClaims.js):

const admin = require("firebase-admin")

admin.initializeApp({
  credential: admin.credential.cert("./serviceAccountKey.json"),
  databaseURL: "https://<project-id>.firebaseio.com/"
})

const uid = "user_uid"

return admin
  .auth()
  .setCustomUserClaims(uid, { admin: true })
  .then(() => {
    // The new custom claims will propagate to the user's ID token the
    // next time a new one is issued.
    console.log(`Admin claim added to ${uid}`)
  });

3.运行脚本

node addClaims.js

您还可以编写一个函数,该函数将一组 UID 作为参数并使用循环向所有这些 UID 添加声明.要详细了解服务帐户和初始化 Admin SDK,请查看此文档.

You can also write a function that takes an array of UIDs as parameter and adds claims to all of them using a loop. To read more about service account and initializing the Admin SDK, check this documentation.

这篇关于没有 Cloud Functions 的 Firebase 自定义声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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