【问题标题】:Trying to set firebase custom claims and are getting errors - JS Backend尝试设置 firebase 自定义声明并收到错误 - JS 后端
【发布时间】:2021-10-28 12:27:46
【问题描述】:

我的代码:

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" 所以我可以使用“import”语句。

【问题讨论】:

    标签: node.js firebase firebase-admin


    【解决方案1】:

    我猜你应该在传递给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);
        });
    

    【讨论】:

      【解决方案2】:

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

      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方法的返回值。像这样:

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

      【讨论】:

      • 我没有使用 OAuth,我使用的是service-account-file 方法。
      • 如果没有详细信息,该评论不是很有帮助。您是否按照此处的说明进行操作:firebase.google.com/docs/admin/…
      • 没关系,别人接了,一针见血:)
      猜你喜欢
      • 2021-10-28
      • 1970-01-01
      • 2019-07-02
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 2019-12-04
      • 2020-07-16
      • 2020-06-01
      相关资源
      最近更新 更多