Firebase-js-sdk: Can I provide a custom persistence implementation for auth in node?

Created on 12 Jun 2019  路  8Comments  路  Source: firebase/firebase-js-sdk

I need to persist the user across sessions in node - is there to do this?

auth internal-bug-filed feature request

Most helpful comment

Bit late to the party, but this workaround worked for me:

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)

All 8 comments

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

That is not currently available. We will consider it in our modularization efforts.

Can you suggest a workaround? Maybe use onIdTokenChanged to save the user and updateCurrentUser on startup to restore? Problem with that is deserializing the user with the correct prototype

Bit late to the party, but this workaround worked for me:

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)

Bit late to the party, but this workaround worked for me:

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)

@JakeHedman I would love to implement this, but at least in my Typescript setup I get the following:

const user = new firebase.User(userData, userData.stsTokenManager, userData);
Property 'User' does not exist on type 'typeof firebase'.ts(2339)

Seems like I can reference the type but not instantiate it. I am using:

"firebase": "^7.14.3",
"firebase-admin": "^8.12.1",

Any workarounds on how to instantiate firebase.User that way with Typescript?

Bit late to the party, but this workaround worked for me:

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)

Could this be documented as a public API so we can rely on it not changing in the future?

Seems to break as of firebase version 7.21.0. Works on firebase version 7.19.1

Hey folks,

I've filed b/169069711 to track this feature request internally. The modularization effort is ongoing - once we complete that we should be in a place where we can consider this feature request.

Hope that helps!
~Malcolm

Was this page helpful?
0 / 5 - 0 ratings

Related issues

suparnavg picture suparnavg  路  3Comments

damonmaria picture damonmaria  路  4Comments

honneygoyal picture honneygoyal  路  4Comments

barakd picture barakd  路  3Comments

Kosai106 picture Kosai106  路  3Comments