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
forcerefresh
boolean to true
? 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
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!
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?
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().
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With