刷新Firebase令牌 [英] Refresh Firebase Token

查看:65
本文介绍了刷新Firebase令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web Firebase javascript通过电子邮件和密码进行身份验证.这个过程会生成一个令牌,我可以使用firebase-admin在我的nodejs后端上进行验证.生成此令牌后,我将其存储在浏览器的本地/会话存储中.

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.

前端是AngularJ,我拦截了http请求以注入存储在浏览器中的令牌.

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

这工作正常,但是一段时间后此令牌过期.那么,在将此令牌发送到nodejs api之前刷新该令牌的最佳方法是什么?

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?

注意:我是否应在每次请求时重试currentUser.getToken()?

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

推荐答案

仅当当前令牌过期时,currentUser.getIdToken() 会刷新令牌!因此,它不会产生不必要的流量或对Firebase的请求!

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

firebase.auth().currentUser.getIdToken(true) // here we force a refresh
.then(function(token) {
  // ... do stuff!
}).catch(function(error) {
  if (error) throw error
});

您将看到我在getIdToken()函数中添加了true作为参数.这会强制刷新!

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

此处是 Firebase文档 getIdToken()功能.

希望对您有帮助!

这篇关于刷新Firebase令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆