【问题标题】:Create a custom token using firebase-admin in NodeJS在 NodeJS 中使用 firebase-admin 创建自定义令牌
【发布时间】:2021-11-14 15:37:50
【问题描述】:

我一直在关注 firebase-admin 的官方文档来创建自定义角色。我希望我的用户只能是医生或普通用户,但它给了我一个错误:

未处理的运行时错误 RangeError: 超出最大调用堆栈大小

const functions = require("firebase-functions");

// The Firebase Admin SDK to access Firestore.
const admin = require("firebase-admin");
admin.initializeApp({
  serviceAccountId:
    "xxxxxx",
});

exports.addMessage = functions.https.onCall((data: any, context: any) => {
  const userId = "some-uid";
  const additionalClaims = {
    role: "doctor",
    name: "Juan",
  };

  admin
    .auth()
    .createCustomToken(userId, additionalClaims)
    .then((customToken: any) => {
      // Send token back to client
      console.log(customToken);
    })
    .catch((error: any) => {
      console.log("Error creating custom token:", error);
    });
});

【问题讨论】:

  • 尝试在.then() 块中添加return { data: customToken }

标签: node.js firebase-authentication google-cloud-functions firebase-admin


【解决方案1】:

Dharmaraj 评论了这个问题:你没有从函数返回任何东西,这意味着没有结果发送给调用者。

exports.addMessage = functions.https.onCall((data: any, context: any) => {
  const userId = "some-uid";
  const additionalClaims = {
    role: "doctor",
    name: "Juan",
  };

  return admin // ? Add return here
    .auth()
    .createCustomToken(userId, additionalClaims)
    .then((customToken: any) => {
      // Send token back to client
      return customToken;  // ? Add this
    })
    .catch((error: any) => {
      // Something went wrong, send error to client
      throw new functions.https.HttpsError('failed-precondition', error); // ? Add this
    });
});

【讨论】:

    猜你喜欢
    • 2017-01-01
    • 2021-10-10
    • 2021-11-17
    • 2020-12-02
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    相关资源
    最近更新 更多