简体   繁体   English

如何从 Flutter 上的 Firebase Auth 获取用户 ID 令牌更改

[英]How to get user ID token changes from Firebase Auth on Flutter

I'm developing a Flutter app using Firebase Auth and for the backend AdonisJS, I already created the sign up/sign in methods, the problem/dude that I have is that the firebase auth token expires after 1 hour and I don't know the correct way to get the new token to send it to the backend to know if the user it authorized to the REST API.我使用ZC047B10EEEE763AFB6164E07CF1CC268Z使用Z035489FF89F892741941941943E415241F5AF9Z AUTH和后端ADONISJS的ADONIS13132323222222222222222222.BOIN/IERM IER ZB。获取新令牌以将其发送到后端以了解其授权给 REST API 的用户的正确方法。

I know I can get the new token with我知道我可以通过

await _firebaseAuth.currentUser.getIdToken()

But how should I check if the token expired and if so, how to ask for the new one?但是我应该如何检查令牌是否过期,如果是,如何要求新的? Is there something like a daemon to check it every time I'm going to consume my REST API每次我要使用我的 REST API 时,是否有类似守护进程的东西来检查它

Your help is really appreciated, I'm pretty new to Firebase and Flutter非常感谢您的帮助,我对 Firebase 和 Flutter 很陌生

With firebase there's no need to store the token and to manage it manually.使用 firebase 无需手动存储和管理令牌。 the web API already manages the session for you and lets you know when there's a change of authorization or login state. web API 已经为您管理了 session 并让您知道何时更改授权或登录 Z69ED639E2AZEA993A5。

You can see how to implement that last mentioned listener here 您可以在此处查看如何实现最后提到的侦听器

If you need to verify if the token is valid in your back end, there's a section in the Node admin SDK where you can check the token.如果您需要验证令牌在后端是否有效,节点管理员 SDK中有一个部分可以检查令牌。

Finally, to retrieve the newest possible token, you can use the method you mentioned like this:最后,要检索最新的可能令牌,您可以使用您提到的方法,如下所示:

firebase.auth().currentUser.getIdToken(/* forceRefresh */ true)

If you want updates to the ID token as it gets automatically refreshed every hour by the Firebase Auth SDK, the documentation briefly explains how to do that:如果您想要更新 ID 令牌,因为它会由 Firebase Auth SDK 每小时自动刷新一次, 文档简要说明了如何执行此操作:

If you need authentication state change events along with any user token refresh events, you can subscribe via the idTokenChanges() method instead.如果您需要身份验证 state 更改事件以及任何用户令牌刷新事件,则可以改为通过idTokenChanges()方法订阅。

It works similar to the authStateChanges() stream in the code provided just above that text.它的工作方式类似于该文本上方提供的代码中的authStateChanges() stream。 It will look something like this:它看起来像这样:

FirebaseAuth.instance
  .idTokenChanges()
  .listen((User user) {
    if (user == null) {
      print('User is currently signed out!');
    } else {
      user.getIdToken().then((String token) {
        print('The user ID token is' + token);
      })
    }
  });

You can store the token globally and use it as much as you want.您可以在全球范围内存储令牌并尽可能多地使用它。 Assuming that this stream keeps putting the new token in that global space, you should not have any problems reusing it.假设这个 stream 不断地将新令牌放在那个全局空间中,那么重用它应该没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM