我需要建议,因为我从未尝试过这种组合:
这个应用程序将成为我的后端并提供一些云功能。
我想使用 google auth2 身份验证并“保护”仅由 android 应用程序调用的云函数,并且仅当 atuh 有效时。
此致
伊万
例如,这是我的“addTickets”场景的云函数:
=== 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;
});
});
======门票.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;
});
}
最佳答案
如果您只希望经过身份验证的用户调用您的可调用函数,那么只需检查 context.auth.uid
存在。如果用户未通过身份验证,则不会有 uid。
关于function - firebase 云函数 - https.onCall(....) 可以使用 Context.Auth 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51841126/