简体   繁体   English

firebase云功能 - https.onCall(...)可以使用Context.Auth吗?

[英]firebase cloud functions - https.onCall(…) can Context.Auth be used?

I need advice becuase I've never tried this combination: 我需要建议,因为我从未尝试过这种组合:

  1. firebase app + realtime database this app will be my backend and provide some cloud functions. firebase app + realtime database这个应用程序将是我的后端并提供一些云功能。
  2. android app which will call these cloud functions. android应用程序将调用这些云功能。

I want to use google auth2 authentication and “protect” the cloud functions to be called by the android app only and if atuh is valid only. 我想使用谷歌auth2身份验证和“保护”云功能只能由Android应用程序调用,如果atuh只有效。

Best Regards Ivan 最诚挚的问候伊万

For expample this is my cloud functions for 'addTickets' scenario: 对于示例,这是我的'addTickets'场景的云函数:

=== index.js: === === index.js:===

exports.addTickets = functions.https.onCall((data, context) => {
 // data comes from client app
 const buyingRecord = data;
 console.log(‘buyingRecord: ‘ + JSON.stringify(buyingRecord));

return tickets.updateTicketsAmmount(buyingRecord)
 .then((result)=>{
 tickets.addTicketsBuyingRecord(buyingRecord);
 result.userid = buyingRecord.userid;
 result.ticketsCount = buyingRecord.ticketsCount;
 return result;
 });
});

====== tickets.js ======= ====== tickets.js =======

exports.updateTicketsAmmount = function(buyingRecord) {
 var userRef = db.ref(‘users/’ + buyingRecord.userid);
 var amountRef = db.ref(‘users/’ + buyingRecord.userid + ‘/ticketsAmount’);
 return amountRef.transaction((current)=>{
 return (current || 0) + buyingRecord.ticketsCount;
 })
 .then(()=>{
 console.log(“amount updated for userid [“ + buyingRecord.userid + “]”);
 return userRef.once(‘value’);
 })
 .then((snapshot)=>{
 var data = snapshot.val();
 console.log(“data for userid [“ + snapshot.key + “]:” + JSON.stringify(data));
 return data;
 });
}

exports.addTicketsBuyingRecord = function(buyingRecord) {
 var historyRef = db.ref(‘ticketsBuyingHistory’);
 var newRecordRef = historyRef.push();
 return newRecordRef.set(buyingRecord)
 .then(()=>{
 console.log(‘history record added.’); 
 return newRecordRef.once(‘value’);
 })
 .then((snapshot)=>{
 var data = snapshot.val();
 console.log(‘data:’ + JSON.stringify(data));
 return data;
 });
}

If you want only authenticated users to invoke your callable function, then simply check that context.auth.uid exists. 如果只希望经过身份验证的用户调用可调用函数,则只需检查context.auth.uid存在。 If the user is not authenticated, there will be no uid. 如果用户未经过身份验证,则不会有uid。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 云 function:同一个 function(onCall)可以服务多少用户? 它可以为他们提供多少资源? - Firebase Cloud function: how many users can the same function (onCall) serve? With how many resources can it serve them? Firebase 云函数在 00 处触发 - Firebase Cloud Functions trigger at 00 Firebase云功能限制子级 - firebase cloud functions limit children 使用Firebase云功能部署/测试功能 - Deploying/Testing functions with Firebase Cloud Functions Firebase HTTPS云功能在生产中返回404 - Firebase HTTPS Cloud Function Returning 404 in Production 如何在 PC 上本地测试 Cloud Functions for Firebase - how to test Cloud Functions for Firebase locally on pc Firebase的云功能:项目结构多个自包含功能 - Cloud Functions for Firebase: Project structure multiple self-contained functions 嵌套函数中的省略号扩展:错误“ ..3在错误的上下文中使用,没有…可查看” - Ellipsis expansion in nested functions: Error “..3 used in an incorrect context, no … to look in” Firebase函数记录使用Cloud Firestore和Node JS的错误 - Firebase functions logs error working with Cloud Firestore and Node JS 嵌套函数中的省略号扩展:错误“'...'在不正确的上下文中使用” - Ellipsis expansion in nested functions: Error “'…' used in an incorrect context”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM