簡體   English   中英

Android 應用程序的 Firebase Cloud Functions 身份驗證

[英]Firebase Cloud Functions authentication for Android App

我正在構建一個搜索附近位置的 Android 應用程序。 I use Firebase login system (Login with email and password, and login with Google, Facebook, etc), therefore I would also like to build an API with Firebase. (也是因為我需要更復雜的應用程序)我已經使用 Firebase Cloud Functions 構建了一個無服務器 API,並且我可以使用 Postman 發出 GET/PUT 請求。 但是,我想保護這些端點,類似於 JWT 如何保護 RESTAPI,以便只有登錄應用程序的用戶才能發出請求。 我如何實現這一目標? 我看過“authorized-https-endpoint”,但它似乎只允許谷歌登錄。

或者有沒有辦法讓我仍然可以使用 Node 和 Mongodb RestAPI,並使用登錄到 Firebase 的帳戶來保護它?

這是一段后端代碼

app.get('/api/read/:item_id', (req, res) => {
(async () => {
    try {
        const document = db.collection('items').doc(req.params.item_id);
        let item = await document.get();
        let response = item.data();
        return res.status(200).send(response);
    } catch (error) {
        console.log(error);
        return res.status(500).send(error);
    }
    })();
});


exports.app = functions.https.onRequest(app);

非常感謝你們。

使用 Firebase 可調用函數。 它們滿足您的要求。

參考: https://firebase.google.com/docs/functions/callable

如果 function 調用存在問題,請參閱:firebase.google.com/docs/functions/callable-reference。
正如這里提到的,這僅在 SDK 不適合您時使用

授權的https-endpoint示例支持客戶端上的所有forms auth,只要它通過Firebase Auth SDK。 In all cases, the client can send an auth token to the function, and the function code can use the Firebase Admin SDK to verify the token . 用戶的身份驗證方式無關緊要 - 任何 Firebase 用戶帳戶都可以使用。

您還可以使用可調用的 function ,它將以完全相同的方式自動為您執行驗證。 然后,您的代碼必須在繼續之前檢查用戶是否使用調用上下文進行了身份驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM