angular - 我是否需要每次都从 Firebase 获取用户令牌
问题描述
我已将以下方法放在Auth
服务上并获取currentUserIdToken
并将其存储为全局变量。这种方法有问题吗?即它会很快过期这个令牌或类似的东西吗?还是我每次都需要从 Firebase 获取这个?我已经采用了这种方法,因为我可以避免对数据库的不必要调用。有什么想法吗?
init(): void {
this.afAuth.authState.subscribe(async (user: firebase.User) => {
if (user) {
this.currentUserUid = user.uid;
this.currentUserIdToken = await this.afAuth.auth.currentUser.getIdToken(true); // Here
this.onboardingService.currentUserUid = this.currentUserUid;
});
}
解决方案
令牌将每小时过期一次。无需getIdToken()
重复调用,您可以使用onIdTokenChanged()监听它的更改,并提供回调以在客户端 SDK 自动刷新时立即接收新令牌。
也可以看看:
推荐阅读
- javascript - 从 url 获取 JSON 数据以使用 jQuery 对 JSON 对象数据进行实时搜索
- python - 用单独的值替换每个匹配项
- c - 为什么我的 levenshtein 距离计算器无法显示 PDF 文件?
- kubernetes - GCP 中的 HELM 部署:尝试进行入口转发规则查找以在 hpa 中使用
- r - 我可以旋转由 igraph::graph 制作的网络图吗?
- javascript - react中的多表单什么都没有显示
- jquery - 使用 Laravel 在 ROUTE 中添加额外参数的 Ajax GET 请求
- python - Python中的Google Sheets API,创建电子表格然后编辑,找不到电子表格
- gsap - ScrollMagic 中有没有办法使 setPin 持续时间等于时间而不是滚动距离?
- php - CKFinder 3 - 浏览器为空白