firebase 访问令牌是否会自动刷新? [英] Is the firebase access token refreshed automatically?

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

问题描述

我正在使用 Firebase 电子邮件/密码身份验证.用户成功登录后,我通过以下方式查询访问令牌:

I'm using the Firebase email/password authentication. After the user has signed in successfully I query the access token the following way:

FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
mUser.getIdToken(true)
    .addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
    public void onComplete(@NonNull Task<GetTokenResult> task) {
        if (task.isSuccessful()) {
            String idToken = task.getResult().getToken();
            // Send token to your backend via HTTPS
            // ...
        } else {
            // Handle error -> task.getException();
        }
    }
});

根据 Firebase 文档,访问令牌会在 1 小时后过期.为了处理在我的应用程序中始终拥有当前访问令牌的情况,我正在寻找一个解决方案,我在 firebase 文档中发现我有 注册一个 Firebase.IdTokenListener 来监听

According to the Firebase documentation the access token expires after 1 hour. To handle the case to have always the current access token in my app, I was looking for a solution and I found in the firebase documentation that I have to register a Firebase.IdTokenListener to listen to the

onIdTokenChanged 事件

onIdTokenChanged event

我的问题是:

onIdTokenChanged 事件

onIdTokenChanged event

访问令牌过期时自动触发?

automatically fired if the access token expired?

如果在访问令牌过期后未自动触发事件,那么使用手动"查询新的/更新的访问令牌的 coorect 方法是什么

In case the event is not automatically fired after the access token has expired, what would be the coorect approach to query "manually" for a new/updated Access token with the

"FirebaseAuth.getInstance().getCurrentUser().getIdToken(boolean)"

方法?

我知道,如果我打电话

mUser.getToken(true)

mUser.getToken(true)

然后触发提到的事件并且

then the mentioned event is fired and the

IdTokenListener

IdTokenListener

被触发.但这不是我要找的.

is triggered. But this is not what I'm looking.

推荐答案

onIdTokenChanged 在 ID 令牌更改时触发.如果 ID 令牌过期,它不会触发.如果刷新新的 ID 令牌、新用户登录或现有用户退出,它将触发.

onIdTokenChanged is triggered anytime the ID token changes. It won't fire if the ID token is expired. It will fire if a new ID token is refreshed, new user signs in or existing user signs out.

Firebase 会根据需要自动刷新.例如,如果您使用实时数据库或 Firestore,它们将在令牌过期后自动刷新令牌,因为它们需要持久连接和 ID 令牌.这将导致该侦听器触发.

Firebase automatically refreshes if it needs to. For example if you are using real time database or Firestore, they will automatically refresh the token after it expires since they require a persistent connection and an ID token for that. This will cause that listener to trigger.

getIdToken() 将缓存未过期的令牌,如果您调用它并检测到过期,它将自动刷新 ID 令牌,从而触发该侦听器.

getIdToken() will cache the unexpired token and if you call it and it detects expiration, it will automatically refresh the ID token which will trigger that listener.

顺便说一句,getToken() 已弃用.您应该改用 getIdToken.

BTW, getToken() is deprecated. You should use getIdToken instead.

这篇关于firebase 访问令牌是否会自动刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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