坚持使用Firebase的Node

坚持使用Firebase的Node

本文介绍了坚持使用Firebase的Node.js客户端应用程序用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase构建Node.js命令行界面(CLI)用于与后端进行身份验证.我想避免让用户在每次运行命令时都键入密码.相反,我想实现一个登录"流程,该流程将持久保存文件系统的凭据,该凭据可用于后续的无密码身份验证,直到用户注销"为止.

I'm building a Node.js command-line interface (CLI) using Firebasefor authentication with the back end. 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".

基本上我正在寻找的是 firebase JavaScript SDK的身份验证状态持久性"功能.不幸的是,Node.js不支持该功能.在模式下用'local''session'调用setPersistence会引发错误当前环境不支持指定的持久性类型."

Basically what I'm looking for is the firebase JavaScript SDK's "auth state persistence" feature. Unfortunately that feature is not supported for 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."

靠我自己"实现该功能的最简单方法是什么?

What's the easiest way to implement that feature "on my own"?

我研究了SDK如何在浏览器中保留用户,并且基本上将用户对象字符串化并将其存储在本地存储中.我可以在Node.js(实例具有toJSON方法)中轻松地自己对用户对象进行字符串化,但是我不知道如何稍后将字符串反序列化为firebase.User实例.我在,看起来就可以解决问题.但这并未在SDK AFAIK上对外公开.

I looked into how the SDK persists the user in a browser and basically it stringifies the user object and stores it localstorage. 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. 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.

推荐答案

您可以从序列化的用户对象实例化firebase.User类:

You can instantiate the firebase.User class from a serialized user object:

保存用户

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

加载用户

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

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

这篇关于坚持使用Firebase的Node.js客户端应用程序用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:38