Ken*_*ong 5 javascript firebase firebase-authentication
我目前正在使用firebase.auth().createUserWithEmailAndPassword(email, password)身份验证用户并使用JWT令牌来firebase.auth().currentUser.getToken(true)获取API请求.但是,Firebase会在1小时后使令牌无效.所以我想知道如何刷新令牌.我是否必须使用自己的自定义令牌生成来正确使用令牌刷新?
我目前正在使用它,虽然我只测试了一次,但它似乎工作.
firebase.auth().onAuthStateChanged(function () { // Refresh token here })
我一直在阅读文档,并没有看到任何提及为Web Apps刷新令牌的提及.我还查看了firebase的示例存储库,并没有看到任何人使用onAuthStateChanged这样的事情.所以我想知道这是客户端令牌刷新的正确方法吗?我觉得这可能不是最好的方法的原因是因为这可能有竞争条件.例如,如果令牌过期,我在刷新令牌之前使用旧令牌发送API请求,那么我的API请求将因令牌过期而失败.
这与Firebase DB HTTP API Auth中的问题非常相似:何时以及如何刷新JWT令牌?但问题是使用Python而没有提到的问题略有不同onAuthStateChanged.
谢谢!
对于那些进入这篇文章寻找答案的人。您可以在所有 API 调用之前获取令牌以获取令牌。
// Make sure you don't pass in true which will always refresh the token
firebase.auth().currentUser.getToken()
Run Code Online (Sandbox Code Playgroud)
Firebase will internally determine if the token needs to be refreshed or grab from cache. If you notice Firebase will not send any network requests until the token is expired.
| 归档时间: |
|
| 查看次数: |
746 次 |
| 最近记录: |