首页 > 解决方案 > 如何从命令行设置 Firebase 自定义声明?

问题描述

人们以前看过类似的问题,但我能找到的最新的是 2018 年。

您如何使用终端在 firebase 中设置自定义声明?

我想执行一个脚本,将具有指定电子邮件的用户设置为管理员。

脚本是:

const admin = require('firebase-admin');
admin.initializeApp()

const adminEmails = [
    "test1@test.com",
    "test2@test.com",
    "test3@test.com"
]

for (let adminEmail of adminEmails) {
    admin.auth().getUserByEmail(adminEmail)
    .then(function(userRecord) {
      // See the UserRecord reference doc for the contents of userRecord.
      console.log('Successfully fetched user data:', userRecord.toJSON());
      admin.auth().setCustomUserClaims(userRecord.uid, {admin: true}).then(() => {
          // The new custom claims will propagate to the user's ID token the
          // next time a new one is issued.
        });
    })
    .catch(function(error) {
     console.log('Error fetching user data:', error);
    });
}

当我使用节点运行此程序时,我得到无效的凭据 - 我如何从命令行进行身份验证,是否可以运行创建这样的自定义声明的脚本?

标签: node.jsfirebasefirebase-authenticationfirebase-admin

解决方案


错误消息表明您需要传递一个带有服务帐户凭据的对象initializeApp()来配置 Admin SDK,或设置GOOGLE_APPLICATION_CREDENTIALSenv var,如文档中所述。


推荐阅读