我想在云函数中创建自定义令牌,但在此之前,我希望检查和比较来自实时数据库的时间戳,并与当前的time.if比较,时间戳在10分钟以下,然后创建自定义令牌并发送回client.please,帮助我实现这一点。
这是我的密码
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请帮助解决这个问题。我不知道我在哪里犯了这个错误。
发布于 2021-04-17 22:25:22
您需要确保Firebase Admin已启动并在函数继续运行之前运行。
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使用客户端上的承诺,并支持“返回对象”示例:
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并使用.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;
}https://stackoverflow.com/questions/67143220
复制相似问题