问题
My app uses Firebase to authenticate users by phone number, a migration from Digits. I add the idToken from Firebase to my calls. I listen with an interceptor on my httpclient if a 401 was trown, if so, I logged out.
I noticed after one hour the 401 came in, so I added an addIdTokenListener in my App class. When it changes I update my token to sign my calls. It worked, but not flawless, sometimes a 401 was thrown and I still logged the user out...
I am writing something in my interceptor to get the IdToken from the user, but the call firebaseUser.getIdToken() is async. So I'm starting to make things complicated, I guess.
Could anyone point me in the right direction? What is your workflow?
回答1:
You're going in the right direction. One thing you may want to do is alter your logic a little based on the reason for the ID token validation failure. You can unpack the ID token data yourself and check the expiration field. If the token has expired, return a different code that triggers the retrieval process, before trying the call again.
来源:https://stackoverflow.com/questions/46322255/catching-401-and-refreshing-firebase-id-token