Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh Firebase Token

I'm using web firebase javascript to authenticate by email and password. This process generates a token which I use to verify on my nodejs backend using firebase-admin. Once this token is generated, I store it on the browser local/session storage.

The front end is AngularJs which I intercept the http request to inject the token stored within the browser.

This is working fine, however after a while this token expire. So what would be the best way to refresh this token before it sends to the nodejs api?

Note: should I requet the currentUser.getToken() every request?

like image 619
Caco Avatar asked May 07 '26 00:05

Caco


1 Answers

The currentUser.getIdToken() only refreshes the token when the current token has expired! So it doesn't create unneeded traffic or requests to Firebase!

firebase.auth().currentUser.getIdToken(true) // here we force a refresh
.then(function(token) {
  // ... do stuff!
}).catch(function(error) {
  if (error) throw error
});

You'll see that I added true as an argument to the getIdToken() function. This forces a refresh!

Here is the Firebase Documentation for the getIdToken() function.

I hope that helps!

like image 101
jsnns Avatar answered May 09 '26 12:05

jsnns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!