简体   繁体   English

如何使用 Flutter 更新 Firebase 中的身份验证令牌

[英]How to update authentication token in Firebase with Flutter

I'm making an app with Flutter and Firebase but I'm having issues with the "email_verified" check in Firebase Rules.我正在使用 Flutter 和 Firebase 制作应用程序,但我在 Firebase 规则中遇到“email_verified”检查问题。 I want to grant read access to documents only if the user has their email verified.我想仅在用户已验证其 email 时授予对文档的读取权限。 The code seems to be correct, but when I create an account in my app and verify my email, I get a permission-denied error when trying to access the data in the documents.代码似乎是正确的,但是当我在我的应用程序中创建一个帐户并验证我的 email 时,在尝试访问文档中的数据时出现权限被拒绝的错误。 I'm only able to read the data if I sign out and in again.如果我退出并再次登录,我只能读取数据。 Therefore, I'm pretty sure the problem is that the auth token isn't being updated.因此,我很确定问题是授权令牌没有更新。

I saw some questions about this (like this one Firebase firestore not updating email verification status ) but I wasn't able to apply the solution to my code, since I'm using Flutter and I wasn't able to find how to do that there.我看到了一些关于此的问题(比如这个Firebase firestore not updating email verification status )但我无法将解决方案应用于我的代码,因为我使用的是 Flutter 并且我无法找到如何做到这一点那里。

Here's the Firebase Rules code that is failing:这是失败的 Firebase 规则代码:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /posts_data/{postId} {
      allow read: if request.auth.token.email_verified;
    }
  }
}

I'm not experienced with Firebase, so I would appreciate it if someone could tell me how exactly to force the auth token to update in Flutter.我没有使用 Firebase 的经验,所以如果有人能告诉我如何在 Flutter 中强制更新身份验证令牌,我将不胜感激。

You can force the token to update in Flutter by calling the reload() method on the FirebaseUser object after the email verification is complete. email验证完成后,可以在FirebaseUser object上调用reload()方法,强制更新Flutter中的token。 Try the following code after email verification done: email 验证完成后尝试以下代码:

FirebaseAuth.instance.currentUser().then((user) async {
  await user.reload();
});

This will update the user's Auth token with the latest information from the Firebase server, including the email verification status.这将使用来自 Firebase 服务器的最新信息更新用户的 Auth 令牌,包括 email 验证状态。

OR或者

You can directly use user.isEmailVerified() when you try to access the unauthorized content just for debugging purposes.And you can also use FirebaseAuth.instance.currentUser().getIdToken() to get the auth token and verify that as well here's the reference for that.当您尝试访问未经授权的内容时,您可以直接使用user.isEmailVerified() FirebaseAuth.instance.currentUser().getIdToken()参考

Source: firebase_auth package资料来源: firebase_auth package

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

相关问题 如何在 Flutter 上刷新 firebase 令牌? - How to refresh firebase token on Flutter? 如何使用 Firebase 在 Flutter 中进行电话身份验证? - How to do Phone Authentication in Flutter using Firebase? 如何捕获 firebase 身份验证多次更新 flutter - How to catch a firebase authentication multiple updates for flutter 使用自定义令牌的 Firebase 身份验证 - Firebase authentication with custom token Flutter App如何在Firebase中集成身份验证和数据库? - How to integrate authentication and database in Firebase for Flutter App? 使用 firebase 和 flutter 进行电话身份验证 - Phone authentication with firebase and flutter 由于 Firebase 电话身份验证的 Flutter 应用程序中出现“令牌不匹配”身份验证错误,Apple 拒绝了该应用程序 - Apple rejected the app due to "Token mismatch" authentication error in a Flutter app with Firebase Phone Authentication 获取用于 firebase 管理员身份验证的令牌 - Get token for firebase admin authentication 如何更新 flutter 中 firebase 火库中的所有集合 - How to update all collection in firebase firestore in flutter 如何在 firebase flutter 中搜索文档并进行更新 - how to search document and update it in firebase flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM