[英]firebase cloud functions - https.onCall(…) can Context.Auth be used?
I need advice becuase I've never tried this combination: 我需要建议,因为我从未尝试过这种组合:
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.