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



我想在云功能中创建自定义令牌,但在此之前,我想检查并比较实时数据库中的时间戳和当前时间。如果时间戳低于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-无参数

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

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

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

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

更新:

所有云功能都应向客户端返回响应onCall在客户端上使用promise并支持"return Object"示例:

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

更新:

所有路径和promise都需要正确解析,这包括等待promise解析并返回其结果,或者存储结果以进行任何二次处理-我建议清理代码,删除try/catch并使用.then().catch()示例:

if (timediff<10 && daydiff==0) {
return await admin.auth().createCustomToken(data)
.then((customtoken)=>{
console.log("auth created"+" "+timediff+" "+daydiff+" "+customtoken);
return customtoken;
})
.catch (err) {
return new functions.https.HttpsError("unknown", err.message, err);
}
} 
else {
console.log("else "+" "+now+" "+reg+" "+time+" "+daydiff);
return "else "+" "+now+" "+reg+" "+time+" "+daydiff;
}

相关内容

  • 没有找到相关文章