function - firebase 云函数 - https.onCall(....) 可以使用 Context.Auth 吗?

标签 function firebase oauth cloud

我需要建议,因为我从未尝试过这种组合:

  • firebase 应用程序 + 实时数据库
    这个应用程序将成为我的后端并提供一些云功能。
  • android 应用程序将调用这些云功能。

  • 我想使用 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/

    相关文章:

    具有不同类型的 HashMap 输入变量的 Java 相同函数名?

    firebase - 如何将 Firebase 实时数据库导出为 CSV?

    node.js - passport-google-oauth 无法注销用户

    oauth - Google 帐户链接操作 - 根据我们的帐户链接政策,Google 帐户不能用作身份验证端点 URL

    android - 如何正确验证 Google token 的完整性?

    python - 为什么在 Python 中不允许使用 foo(*arg, x)?

    javascript - 为什么这个 javascript 函数返回未定义?

    函数只带出sas中的小数

    ios - 在iOS上下载应用程序后,Firebase深层链接无法跟踪链接(延迟的深层链接)

    java - 将 firebase 添加到应用程序时出现 appcompat 依赖项错误