尝试设置 firebase 自定义声明并出现错误 - JS 后端

Trying to set firebase custom claims and are getting errors - JS Backend

我的代码:

import admin from "firebase-admin";

const path = `./service-account-key.json`;

admin.initializeApp({
    credential: path,
});


// Get uid from user email

var uid = "";
const email = "bearcodes@outlook.com";

admin.auth().getUserByEmail(email)
    .then(function (userRecord) {
        uid = userRecord.uid;
        console.log(userRecord.uid)
    })
    .catch(function (error) {
        console.log("Error fetching user data:", error);
    });

这是我不断收到的错误:

C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\utils\error.js:44
        var _this = _super.call(this, errorInfo.message) || this;
                           ^

FirebaseAppError: Invalid Firebase app options passed as the first argument to initializeApp() for the app named "[DEFAULT]". The "credential" property must be an object which implements the Credential interface.
    at FirebaseAppError.FirebaseError [as constructor] (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\utils\error.js:44:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\utils\error.js:90:28)
    at new FirebaseAppError (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\utils\error.js:125:28)
    at new FirebaseApp (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\firebase-app.js:134:19)
    at FirebaseNamespaceInternals.initializeApp (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\firebase-namespace.js:77:19)
    at FirebaseNamespace.initializeApp (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\firebase-namespace.js:387:30)
    at file:///C:/Users/bearc/Downloads/flaskTesting/applycustomclaims.js:5:7
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5) {
  errorInfo: {
    code: 'app/invalid-app-options',
    message: 'Invalid Firebase app options passed as the first argument to initializeApp() for the app named "[DEFAULT]". The "credential" property must be an object which implements the Credential interface.'
  },
  codePrefix: 'app'
}

谁能帮我看看这到底是怎么回事? 我已经用这个参数设置了我的 package.json 文件:"type": "module" 所以我可以使用“导入”语句。

正如您在错误消息中看到的那样,credential 属性 不应是字符串(这是您传递的内容)。它是一个具有 属性: getAccessToken 的 Credential 对象,它是一个 return 访问令牌对象的函数。

这是 Credential 接口的定义:

interface Credential {

    /**
     * Returns a Google OAuth2 access token object used to authenticate with
     * Firebase services.
     *
     * This object contains the following properties:
     * * `access_token` (`string`): The actual Google OAuth2 access token.
     * * `expires_in` (`number`): The number of seconds from when the token was
     *   issued that it expires.
     *
     * @return A Google OAuth2 access token object.
     */
    getAccessToken(): Promise<admin.GoogleOAuthAccessToken>;
  }

更新: 根据 docs,如果您使用的是服务帐户,credential 字段的值应该是 applicationDefault 方法的 return 值。像这样:

admin.initializeApp({
  credential: admin.credential.applicationDefault()
});

我猜你应该在传递给 admin.initializeApp(...)

之前使用 admin.credential.cert(serviceAccountPathOrObject: string | admin.ServiceAccount) 包装凭据
import admin from "firebase-admin";

const path = `./service-account-key.json`;

admin.initializeApp({
    credential: admin.credential.cert(path),
});


// Get uid from user email

var uid = "";
const email = "bearcodes@outlook.com";

admin.auth().getUserByEmail(email)
    .then(function (userRecord) {
        uid = userRecord.uid;
        console.log(userRecord.uid)
    })
    .catch(function (error) {
        console.log("Error fetching user data:", error);
    });