微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从实时数据库中获取数据后,在云函数中创建自定义令牌

如何解决从实时数据库中获取数据后,在云函数中创建自定义令牌

我想在云函数中创建自定义令牌,但在此之前我想检查和比较来自实时数据库和当前时间的时间戳。如果时间戳低于 10 分钟,那么我想创建自定义令牌并将其发送回客户端。请帮助我实现这一目标。我是此云功能 Firebase 的新手。

这是我的代码

export const authaccount = functions.https.onCall(async (data) => {
  try {
    const snap= await admin.database().ref("/register/"+data).get();
    const time=snap.val().timestamp;
    const Now=new Date().getDate();
    const reg=new Date(time).getDate();
    const today=Math.abs(Now-reg);
    const daydiff=Math.floor(today/1000/60/60/24);
    const Nowminutes=new Date().getUTCMinutes();
    const regminutes=new Date(time).getUTCMinutes();
    const timediff=Math.abs(Nowminutes-regminutes);
    if (timediff<10 && daydiff==0) {
      try {
        admin.auth().createCustomToken(data).then((customtoken)=>{
          console.log("auth created"+" "+timediff+" "+daydiff+" "+customtoken);
          return customtoken;
        });
      } catch (err1) {
        throw new functions.https.HttpsError("unkNown",err1.message,err1);
      }
    } else {
      console.log("else "+" "+Now+" "+reg+" "+time+" "+daydiff);
    }
  } catch (err2) {
    throw new functions.https.HttpsError("unkNown",err2.message,err2);
  }
});
2:53:20.626 AM
authaccount
Error: Process exited with code 16 at process.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:275:22) at process.emit (events.js:314:20) at process.EventEmitter.emit (domain.js:483:12) at process.exit (internal/process/per_thread.js:168:15) at Object.sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:37:9) at process.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:271:22) at process.emit (events.js:314:20) at process.EventEmitter.emit (domain.js:483:12) at processpromiseRejections (internal/process/promises.js:209:33) at processticksAndRejections (internal/process/task_queues.js:98:32)
2:53:19.559 AM
authaccount
Error: The caller does not have permission; Please refer to https://firebase.google.com/docs/auth/admin/create-custom-tokens for more details on how to use and troubleshoot this feature. at FirebaseAuthError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:44:28) at FirebaseAuthError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:90:28) at new FirebaseAuthError (/workspace/node_modules/firebase-admin/lib/utils/error.js:149:16) at Function.FirebaseAuthError.fromServerError (/workspace/node_modules/firebase-admin/lib/utils/error.js:188:16) at /workspace/node_modules/firebase-admin/lib/auth/token-generator.js:114:53 at processticksAndRejections (internal/process/task_queues.js:97:5) at async Promise.all (index 1)
2:53:19.558 AM
authaccount
Unhandled rejection
2:53:19.469 AM
authaccount
Function execution took 1386 ms,finished with status code: 200

请帮忙解决这个问题。我不知道我在哪里犯了错误

解决方法

您需要确保您的 Firebase Admin sdk 已启动并在该功能继续运行之前运行

if (firebase.apps.length === 0) {
    firebase.initializeApp();
}

资源:https://firebase.google.com/docs/admin/setup#initialize-without-parameters

我怀疑您是否修改了服务帐户的 IAM 权限,但正如评论所建议的那样:https://firebase.google.com/docs/auth/admin/create-custom-tokens#service_account_does_not_have_required_permissions

一旦确认有效 - 您需要确保 onCall data 是一个字符串而不是 null,一些简单的健康检查可以帮助您调试流程

console.log(typeof data);
console.warn("Data",data);

从那里我还会调试您的日期时间和实时数据库结果,这些是异步的,需要在您使用它之前解决承诺。

更新:

所有云函数都应该向客户端返回响应 onCall 在客户端使用 promise 并支持“返回对象” 例子:

return {
  token: myCustomToken,possible: otherValue
};

为了比较,onRequest 使用了类似 fetch 的响应并支持代码

response.status(500)
response.send({name:value})
return;

来源: https://firebase.google.com/docs/functions/callable#sending_back_the_result

来源: https://firebase.google.com/docs/functions/http-events#using_express_request_and_response_objects

更新:

所有路径和承诺都需要正确解析,这包括等待承诺解析并返回其结果或存储结果以供任何二次处理 - 我建议清理代码,删除 try/catch 并使用 {{1 }} 示例:

.then().catch()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?