Catching 401 and refreshing Firebase Id Token

爱⌒轻易说出口 提交于 2019-12-24 08:06:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!