-
Notifications
You must be signed in to change notification settings - Fork 938
onIdTokenChanged never called when token refreshes #2985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm sad to hear that your experience with using Firebase was poor. To help you relive the good times I've assigned sam to this bug instead. |
Hi Joel, The token refreshes lazily when interacting with other Firebase services (or if you call |
Yeah, I guess that wasn't clear from reading the documentation. You may want to disclose that and even put some more color around it on how tokens get refreshed. Here's what we ended up doing to get this fixed: const getCurrentUserAuthorization = async () => {
const currentUser = firebase.auth().currentUser
if (currentUser) {
const token = await currentUser.getIdToken()
return `Bearer ${token}`
} else {
return ''
}
} Fortunately, the GraphQL library that we're using supports asynchronous headers (it'll resolve them before making a request), so this ended up working. But, I initially assumed that Firebase would be automatically keeping my token refreshed for me. Maybe update the docs as an action item? |
Yep, that seems reasonable. I'll follow up with the documentation. I'll also raise some sort of auto-renewal feature request with the team as something to consider in the future. |
@samhorlbeck Just ran into this issue with |
Does anyone know the lazy time of "onIdTokenChanged" listener? |
Could you also update the docs with all the different ways
Reading through the source, it appears there could be more cases, and it's not clear how to identify these errors on the client (what are the Given that It'd be helpful to update the sample code here https://firebase.google.com/docs/auth/admin/verify-id-tokens#retrieve_id_tokens_on_clients with the various situations as well. |
@sshquack This is something we've not yet had a chance to do. The request is being tracked internally (b/178638982) @kuangthien There lazy refresh that occurs when the Auth library is being used by other Firebase SDKs is dynamic based on when the token is set to expire. You can read through the relevant code here: https://github.com/firebase/firebase-js-sdk/blob/master/packages/auth/src/proactiverefresh.js @WestonThayer Thanks for the suggestion. Would you mind opening a separate feature request? |
@samhorlbeck sure, opened #4358 |
Thnx u for very detailed explaination. And in serverside, whenever the user change s.thing, it also have to write into that Firebase Realtime DB path :D |
the problem is still relevant |
same for me |
A docs change is still pending here, we will look into that. The idToken can be manually refreshed with forceRefresh: true. |
Same for me, the document should be update. @sam-gc: It seems we need to enable Security Token Service API to let getIdToken(true) auto refresh, right? |
That's correct. |
@annp87 could you please explain how to do that? |
@amin79: From my understanding you've just need to enable Token Service API from google cloud API, then you should call getIdTokenID(true) as https://firebase.google.com/docs/auth/admin/verify-id-tokens#web. |
Perhaps this is a bug or undocumented behavior with the emulators, but when I call In my use case I just want a callback any time the firebase token refreshes so that I can refresh another custom token that depends upon the firebase token. @sam-gc @scottcrossen seeing as this has been open for nearly 3 years and the behavior is still unclear could you possibly jot down some notes about the behavior here in a comment? That would at least provide some useful reference until whenever the docs might get updated. |
The docs were updated in #7054, hence marking as closed. Can you confirm if this specific issue only happens in emulators?
|
[REQUIRED] Describe your environment
[REQUIRED] Describe the problem
I’m running into an issue with the JS SDK and the onIdTokenChanged method (https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onidtokenchanged). Specifically, it doesn’t appear to get called when the token refreshes.
We use the Firebase Auth JWT in all requests to our API. If I leave a browser open for an extended period of time, the token never refreshes, and ultimately I start getting this error when trying to decode it:
Error: Firebase ID token has expired. Get a fresh ID token from your client app and try again (auth/id-token-expired). See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.
I’ve done extensive logging around this method and it appears that the token expires and onIdTokenChanged never gets called. Is this expected behavior?
Steps to reproduce:
I have a very vanilla implementation of Firebase Auth. I haven't changed any setting or changed the way persistence is done.
Relevant Code:
The text was updated successfully, but these errors were encountered: