Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When shall we set FirebaseUser.getToken(Boolean forcerefresh) to true?

Tags:

My Firebase client app communicates with a custom back-end server, so I need to send the ID Token with every HTTPS request to my server. From the reference,

Should only be set to true if the token is invalidated out of band
  1. When shall I set the forcerefresh boolean to true?
  2. Is it okay to get the token(using forcerefresh as false) during every HTTPS request to my custom server. (As it returns as a task, Shall i store it in my local cache, so that i don't have to add any task listener and can process the request in the same thread )

EDIT: Now google provides new api which will refresh the token automatically if it is expired.

getIdToken(forceRefresh) returns firebase.Promise containing string

From the Google Docs

Returns the current token if it has not expired, otherwise this will refresh the token and return a new one

like image 275
Bikash Avatar asked Oct 20 '16 12:10

Bikash


People also ask

How do I refresh a firebase token?

Then whenever onTokenRefresh () is called, you'd call FirebaseInstanceID.getToken () again, get a new token, and send that up to the server (probably including the old token as well so your server can remove it, replacing it with the new one). So deleting the FirebaseInstanceId will refresh the token, thanks!

How to call ontokenrefresh from Firebase instance?

If somebody wants to call onTokenRefresh could delete the token and then call FirebaseInstanceId.getInstance ().getToken (). Operation FirebaseInstanceId.getInstance ().deleteInstanceId () need to be in AsyncTask or new Thread, it could not be on the MainThread!!! Why not just call FirebaseInstanceId.getToken?

How to call ontokenrefresh() method if there is no token?

FireBase is clever and it will call onTokenRefresh() method, only if there hasn't token (it is deleted or it is called for the first time) or something else happen and token has been changed. If somebody wants to call onTokenRefresh could delete the token and then call FirebaseInstanceId.getInstance().getToken().

How do I force the ontokenrefresh() method?

Then, retrieve the token from storage after a user logs in and register the token with your server as needed. If you would like to manually force the onTokenRefresh (), you can create an IntentService and delete the token instance. Then, when you call getToken, the onTokenRefresh () method will be called again.


2 Answers

In general, set forceRefresh to false. That is the recommended behavior. However, that depends on what your are doing. You may want to ensure your token is up to date at all times and available synchronously. To do so you have to refresh a couple of minutes before the the token expires. If you call getToken(false), you will get back the cached token which is about to expire which is not desired. In that case, you force a refresh and update the token. By doing so, you ensure you have an unexpired token at all times synchronously.

like image 165
bojeil Avatar answered Sep 23 '22 01:09

bojeil


Set it to false unless you want to force refresh it. If you set it to false, Firebase fetches the cached token(if it has not expired) or refreshes the token and returns it(if it has expired).

https://firebase.google.com/docs/reference/js/firebase.User#getToken

like image 32
Shubham Maheshwari Avatar answered Sep 22 '22 01:09

Shubham Maheshwari