[英]Firebase Authentication for Realm Cloud using JWT and a Google Cloud Function
I am attempting to use JWTs to authenticate into Realm Cloud.我正在尝试使用 JWT 对 Realm Cloud 进行身份验证。 I am using Firebase as the authentication service and am attempting to create a Google Cloud Function to generate the JWT.
我使用 Firebase 作为身份验证服务,并尝试创建一个 Google Cloud 函数来生成 JWT。 I generated the private and public keys using the terminal command "ssh-keygen".
我使用终端命令“ssh-keygen”生成了私钥和公钥。 Realm's JWT tutorial suggested the following line of code to read the key file:
Realm 的 JWT 教程建议使用以下代码行来读取密钥文件:
const key = fs.readFileSync('./functions/id_rsa', 'utf8');
I copied the private key over to the project, pointed the code above to the file, but when I deploy the Google Cloud Function, I received the following error message:我将私钥复制到项目中,将上面的代码指向文件,但是当我部署 Google Cloud Function 时,收到以下错误消息:
⚠ functions[myAuthFunction(us-central1)]: Deployment error.
⚠ 函数 [myAuthFunction(us-central1)]:部署错误。 Function failed on loading user code.
函数加载用户代码失败。 Error message: Code in file index.js can't be loaded.
错误消息:无法加载文件 index.js 中的代码。 Is there a syntax error in your code?
您的代码中是否存在语法错误? Detailed stack trace: Error: ENOENT: no such file or directory, open './functions/id_rsa'
详细的堆栈跟踪:错误:ENOENT:没有这样的文件或目录,打开“./functions/id_rsa”
My project structure is as follows: Picture我的项目结构如下:图片
I have attempted to ask on Realm's forums but I haven't received much help.我曾尝试在 Realm 的论坛上提问,但没有得到太多帮助。 The entire cloud function they suggested is:
他们建议的整个云功能是:
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 }
});
In summary, how can the google cloud function read my private key file in my project?综上所述,google云功能如何读取我项目中的私钥文件? The public key is stored inside my Realm Cloud dashboard for the specific instance.
公钥存储在我的 Realm Cloud 仪表板中,用于特定实例。
Sources: Realm Cloud JWT Firebase tutorial来源: Realm Cloud JWT Firebase 教程
Try passing just ./id_rsa
or id_rsa
.尝试只传递
./id_rsa
或id_rsa
。 I believe the path root is where the index.js file is.我相信路径 root 是 index.js 文件所在的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.