简体   繁体   English

为 Node.js 客户端应用程序保留 Firebase 用户

[英]Persist Firebase user for Node.js client application

I'm building a Node.js command-line interface (CLI) using Firebase for authentication with the back end.我正在构建一个 Node.js 命令行界面 (CLI),使用 Firebase 对后端进行身份验证。 I want to avoid making the user type their password every time they run a command.我想避免让用户在每次运行命令时都输入密码。 Instead, I want to implement a "login" flow that persists a credential to the filesystem that can be used for subsequent password-less authentication until the user "logs out".相反,我想实现一个“登录”流程,该流程将凭证持久保存到文件系统,该凭证可用于后续的无密码身份验证,直到用户“注销”为止。

Basically what I'm looking for is the firebase JavaScript SDK's "auth state persistence" feature.基本上我正在寻找的是firebase JavaScript SDK 的“auth state persistence”功能。 Unfortunately that feature is not supported for Node.js;不幸的是,Node.js 不支持该功能; calling setPersistence with either 'local' or 'session' as the mode raises an error "The current environment does not support the specified persistence type."使用'local''session'作为模式调用setPersistence会引发错误“当前环境不支持指定的持久性类型。”

What's the easiest way to implement that feature "on my own"? “自己”实现该功能的最简单方法是什么?

I looked into how the SDK persists the user in a browser and basically it stringifies the user object and stores it localstorage.我研究了 SDK 如何将用户持久保存在浏览器中,基本上它将用户对象字符串化并将其存储在本地存储中。 I can stringify the user object myself easily enough in Node.js (the instance has a toJSON method), but I can't figure out how to later de-serialize the string into an instance of firebase.User .我可以在 Node.js 中自己轻松地对用户对象进行字符串化(该实例具有toJSON方法),但我无法弄清楚稍后如何将字符串反序列化为firebase.User的实例。 I see this function in the source code that looks like it'd do the trick.在源代码中看到这个函数,看起来它可以解决问题。 But that's not exposed externally on the SDK AFAIK.但这并没有在 SDK AFAIK 上公开。

You can instantiate the firebase.User class from a serialized user object:您可以从序列化的用户对象实例化firebase.User类:

Save user保存用户

const userJson = JSON.stringify(currentUser.toJSON())
// Write userJson to disk

Load user加载用户

// Read userJson from disk
const userData = JSON.parse(userJson)
const user = new firebase.User(userData, userData.stsTokenManager, userData)
firebase.auth().updateCurrentUser(user)

Source: https://github.com/firebase/firebase-js-sdk/issues/1874#issuecomment-549085119来源: https ://github.com/firebase/firebase-js-sdk/issues/1874#issuecomment-549085119

In CLI applications that require tokens it is common for these tokens to be stored somewhere on the local machine (often in a "dot" file in the home directory for Linux machines - eg ~/.yourapp/config .在需要令牌的 CLI 应用程序中,这些令牌通常存储在本地机器的某个位置(通常在 Linux 机器的主目录中的“点”文件中 - 例如~/.yourapp/config

These files can be in any format you want but I like JSON or YAML to store things like this.这些文件可以是你想要的任何格式,但我喜欢 JSON 或 YAML 来存储这样的东西。

In terms of the User object you can easily load the string with the Node.js builtin JSON.parse(yourStringUser) .就用户对象而言,您可以使用 Node.js 内置的JSON.parse(yourStringUser)轻松加载字符串。

Within the firebase-js-sdk source code you can find references where similar is performed with session storage;firebase-js-sdk 源代码中,您可以找到对会话存储执行类似操作的参考资料; using the toJSON() method to set with a certain key and retrieving that value with the JSON.parse() method.使用toJSON()方法设置某个key并使用JSON.parse()方法检索该值。

Session management with service workers 服务工作者的会话管理

end users will have Node.js installed locally - and your wanting to build an interactive Node script/app which requires client/token access to Firebase.最终用户将在本地安装 Node.js - 并且您想要构建一个交互式 Node 脚本/应用程序,这需要客户端/令牌访问 Firebase。

Firebase Auth provides the ability to use service workers to detect and pass Firebase ID tokens for session management. Firebase Auth 提供了使用服务工作者检测和传递 Firebase ID 令牌以进行会话管理的能力。

If you look at how the Firebase CLI itself works, users log in via a web browser then paste an auth code into the Firebase CLI ( firebase login --no localhost ).如果您查看 Firebase CLI 本身的工作原理,用户会通过 Web 浏览器登录,然后将授权代码粘贴到 Firebase CLI ( firebase login --no localhost )。 Then, the user is signed in. So, use a browser and the Firebase Web SDK to collect the credentials.然后,用户登录。因此,使用浏览器和 Firebase Web SDK 来收集凭据。

I explored this exhaustively with Google Cloud Platform support, and we determined that this is not possible with the current SDK.我在 Google Cloud Platform 支持下对此进行了详尽的探索,我们确定这在当前的 SDK 中是不可能的。 FWIW, this feature took me just a few hours to implement using AWS Cognito. FWIW,使用 AWS Cognito 只花了我几个小时就实现了这个功能。 There the CognitoUser constructor allows the user to pass their favorite Node.js implementation of the localStorage API. CognitoUser 构造函数允许用户传递他们最喜欢的 localStorage API 的 Node.js 实现。

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

相关问题 Firebase 错误:Function 加载用户代码失败(node.js) - Firebase error: Function failed on loading user code (node.js) 如何将 Firebase 连接到 Node.JS 项目? - How to connect Firebase to a Node.JS project? Firebase 存储 | 从 node.js 云函数上传时文件是“application/octet-stream” - Firebase Storage | File is "application/octet-stream" on upload from node.js cloud function 如何使用 Node.js 和“firebase-admin”SDK 查询 Firebase? - How to query Firebase with Node.js and "firebase-admin" SDK? 持久化用户Auth Flutter Firebase - Persist user Auth Flutter Firebase 使用 node.js 部署 firebase 时出现错误 - Getting an error when deploying firebase with node.js 如何使用 Node.js 上的 blob 将图像存储在 firebase 存储上 - How to store image on firebase storage with blob on Node.js 从 Node.js 中的 Firebase 实时数据库中选择特定属性 - Select specific properties from Firebase Realtime Database in Node.js 如何在 react(+reactfire) 中使用 firebase 持久化用户身份验证 - How to persist the user authentication with firebase in react(+reactfire) 如何从 Node.js 中获取 Firebase 中的默认存储桶名称? - How do I get the default bucket name in Firebase, from inside Node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM