Reputation: 1235
I'm building a Node.js command-line interface (CLI) using Firebase for 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".
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"?
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.
Upvotes: 6
Views: 2105
Reputation: 6535
You can instantiate the firebase.User
class from a serialized user object:
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
Upvotes: 5
Reputation: 1235
I explored this exhaustively with Google Cloud Platform support, and we determined that this is not possible with the current SDK. FWIW, this feature took me just a few hours to implement using AWS Cognito. There the CognitoUser constructor allows the user to pass their favorite Node.js implementation of the localStorage API.
Upvotes: 1
Reputation: 18545
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.
Firebase Auth provides the ability to use service workers to detect and pass Firebase ID tokens for session management.
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
). Then, the user is signed in. So, use a browser and the Firebase Web SDK to collect the credentials.
Upvotes: 1
Reputation: 15058
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 - e.g. ~/.yourapp/config
.
These files can be in any format you want but I like JSON or YAML to store things like this.
In terms of the User object you can easily load the string with the Node.js builtin JSON.parse(yourStringUser)
.
Within the firebase-js-sdk source code you can find references where similar is performed with session storage; using the toJSON()
method to set with a certain key
and retrieving that value with the JSON.parse()
method.
Upvotes: 1