没有 Cloud Functions 的 Firebase 自定义声明

use*_*019 4 firebase firebase-authentication google-cloud-functions firebase-admin

有没有一种方法可以创建自定义索赔而无需通过云功能?

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

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

Dha*_*raj 7

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

1.创建NodeJS项目

npm init -y

//Add Firebase Admin
npm install firebase-admin
Run Code Online (Sandbox Code Playgroud)

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}`)
  });
Run Code Online (Sandbox Code Playgroud)

3.运行脚本

node addClaims.js
Run Code Online (Sandbox Code Playgroud)

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