在 NodeJS 中使用 firebase-admin 创建自定义令牌

Create a custom token using firebase-admin in NodeJS

我一直在关注 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);
    });
});

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
    });
});