Refresh Firebase Token

╄→尐↘猪︶ㄣ 提交于 2019-12-11 03:25:46

问题


I'm using web firebase javascript to authenticate by email and password. This process generates a token which I use to verify on my nodejs backend using firebase-admin. Once this token is generated, I store it on the browser local/session storage.

The front end is AngularJs which I intercept the http request to inject the token stored within the browser.

This is working fine, however after a while this token expire. So what would be the best way to refresh this token before it sends to the nodejs api?

Note: should I requet the currentUser.getToken() every request?


回答1:


The currentUser.getToken(true) only refreshes the token when the current token has expired! So it doesn't create unneeded traffic or requests to Firebase!

firebase.auth().currentUser.getToken(true)
.then(function(idToken) {
  // ... do stuff!
}).catch(function(error) {
  if (error) throw error
});

Here you'll see that I added true as an argument to the getToken() function. This forces a refresh!

Here is the Firebase Documentation for the getToken() function.

I hope that helps!



来源:https://stackoverflow.com/questions/42616318/refresh-firebase-token

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