简体   繁体   English

使用 JWT 和 Google Cloud 函数对 Realm Cloud 进行 Firebase 身份验证

[英]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_rsaid_rsa I believe the path root is where the index.js file is.我相信路径 root 是 index.js 文件所在的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的谷歌云 function 中验证 firebase jwt 令牌? - How to validate the firebase jwt token in my google cloud function? Firebase 身份验证和谷歌云翻译 - Firebase Authentication and Google Cloud Translate firebase 路由上 golang 中的 google cloud function gen2 的身份验证问题 - Authentication problem with google cloud function gen2 in golang on firebase routing 是否有身份验证,例如在Google Firebase中,甚至在Google Cloud平台中? - Is there authentication, as in Google Firebase even in the Google Cloud platform? 来自Google Cloud Endpoint的Firebase身份验证 - Firebase authentication from Google Cloud Endpoint 具有Firebase和多个云项目的Android Google身份验证 - Android Google Authentication With Firebase And Multiple Cloud Projects 如何在云功能中了解Firebase身份验证的提供者 - how to know the provider of firebase authentication in a cloud function Firebase 云函数身份验证触发时间 - Firebase Cloud Function Authentication Trigger timing 使用 Google Cloud Function 将数据从 firebase firestore 导出到 MeiliSearch - Exporting data from firebase firestore to MeiliSearch using Google Cloud Function 使用Google Firebase云功能下载或查看文件 - Download or view file using google firebase cloud function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM