[英]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.