簡體   English   中英

使用 JWT 和 Google Cloud 函數對 Realm Cloud 進行 Firebase 身份驗證

[英]Firebase Authentication for Realm Cloud using JWT and a Google Cloud Function

我正在嘗試使用 JWT 對 Realm Cloud 進行身份驗證。 我使用 Firebase 作為身份驗證服務,並嘗試創建一個 Google Cloud 函數來生成 JWT。 我使用終端命令“ssh-keygen”生成了私鑰和公鑰。 Realm 的 JWT 教程建議使用以下代碼行來讀取密鑰文件:

const key = fs.readFileSync('./functions/id_rsa', 'utf8'); 

我將私鑰復制到項目中,將上面的代碼指向文件,但是當我部署 Google Cloud Function 時,收到以下錯誤消息:

⚠ 函數 [myAuthFunction(us-central1)]:部署錯誤。 函數加載用戶代碼失敗。 錯誤消息:無法加載文件 index.js 中的代碼。 您的代碼中是否存在語法錯誤? 詳細的堆棧跟蹤:錯誤:ENOENT:沒有這樣的文件或目錄,打開“./functions/id_rsa”

我的項目結構如下:圖片

我曾嘗試在 Realm 的論壇上提問,但沒有得到太多幫助。 他們建議的整個雲功能是:

const functions = require("firebase-functions");
const jwt = require('jsonwebtoken');
const fs = require('fs');
const key = fs.readFileSync(’pathToMyPrivateKeyFile');
exports.myAuthFunction = functions.https.onCall((data, context) => {    
    const uid = context.auth.uid 
    const payload = { userId: uid }    
    const token = jwt.sign(payload, { key: key, passphrase: "your-passphrase" }, { algorithm: 'RS256'}),    
    return { token:token }
});

綜上所述,google雲功能如何讀取我項目中的私鑰文件? 公鑰存儲在我的 Realm Cloud 儀表板中,用於特定實例。

來源: Realm Cloud JWT Firebase 教程

嘗試只傳遞./id_rsaid_rsa 我相信路徑 root 是 index.js 文件所在的位置。

暫無
暫無

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

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