“firebase_auth/custom-token-mismatch” 该自定义令牌对应不同的受众群。

huangapple go评论71阅读模式
英文:

firebase_auth/custom-token-mismatch The custom token corresponds to a different audience

问题

I have a Flutter application in production with many users, using Firebase Backend. I want to add Firebase authentication to it by generating a custom token via a google cloud function.

我有一个Flutter应用程序在生产中,有许多用户,使用Firebase后端。我想通过使用Google云函数生成自定义令牌来为其添加Firebase身份验证。

I created another Firebase project as a dev environment and implemented and tested Firebase auth via a NodeJS cloud function, everything is great, and now I deployed the cloud function to the production environment.

我创建了另一个Firebase项目作为开发环境,并通过NodeJS云函数实现和测试了Firebase身份验证,一切都很顺利,现在我将云函数部署到了生产环境。

I don't use any API Google API keys in NodeJS since all cloud functions are authenticated automatically on GCP. In Flutter, I replaced the dev environment google-services.json with the correct one in production. I don't have any GoogleService-Info, I'm testing on Android.

我在NodeJS中不使用任何Google API密钥,因为所有云函数在GCP上都会自动进行身份验证。在Flutter中,我将开发环境的google-services.json替换为生产环境的正确文件。我没有任何GoogleService-Info,我正在Android上进行测试。

In main.dart, I initialize my app like this

main.dart中,我这样初始化我的应用程序

  if (Firebase.apps.isEmpty) {
    try {
      await Firebase.initializeApp(
        name: "foo",
        options: DefaultFirebaseOptions.currentPlatform,
      );
    } catch (e) {
      print("could not initialize firebase ${e}");
    }
  }

options: DefaultFirebaseOptions.currentPlatform, is a file generated by FlutterFire CLI, it contains, the production app credentials, this is the only file I forgot to change when I was doing the dev environment testing, I forgot to create new apps for development and change the values in firebase_options.dart

options: DefaultFirebaseOptions.currentPlatform是由FlutterFire CLI生成的文件,它包含生产应用程序的凭据,在进行开发环境测试时,这是唯一一个我忘记更改的文件,我忘记为开发创建新应用程序并在firebase_options.dart中更改值。

Now what's happening is, the cloud function is generating the token correctly but whenever I try to sign in via this token in Flutter

现在发生的情况是,云函数正确生成了令牌,但每当我尝试在Flutter中使用此令牌登录时,

await FirebaseAuth.instance.signInWithCustomToken(firebase_token);

I get this error

我收到以下错误

flutter (14645): Authentication error [firebase_auth/custom-token- 
mismatch] The custom token corresponds to a different audience.

Authentication error [firebase_auth/custom-token- mismatch] The custom token corresponds to a different audience.

身份验证错误[firebase_auth/custom-token- mismatch],自定义令牌对应于不同的受众。

How to fix this error? I can't just create new Firebase apps, if that's the root cause of the error, for two reasons

如何修复此错误?如果这是错误的根本原因,我不能只是创建新的Firebase应用程序,有两个原因:

  1. I can't delete the existing because existing users won't be able to use the application

  2. 我不能删除现有的应用程序,因为现有用户将无法使用该应用程序

  3. I can't create new apps, because the Android package name is required, which is used by the existing apps.

  4. 我不能创建新的应用程序,因为需要Android包名称,这是现有应用程序使用的。

So I want a solution that doesn't require disturbing existing users, if possible.

因此,如果可能的话,我想要一个不会干扰现有用户的解决方案。

英文:

I have a Flutter application in production with many users, using Firebase Backend. I want to add Firebase authentication to it by generating a custom token via a google cloud function.

I created another Firebase project as a dev environment and implemented and tested Firebase auth via a NodeJS cloud function, everything is great, and now I deployed the cloud function to the production environment.

I don't use any API Google API keys in NodeJS since all cloud functions are authenticated automatically on GCP. In Flutter, I replaced the dev environment google-services.json with the correct one in production. I don't have any GoogleService-Info, I'm testing on Android.

In main.dart, I initialize my app like this

  if (Firebase.apps.isEmpty) {
    try {
      await Firebase.initializeApp(
        name: "foo",
        options: DefaultFirebaseOptions.currentPlatform,
      );
    } catch (e) {
      print("could not initialize firebase ${e}");
    }
  }

options: DefaultFirebaseOptions.currentPlatform, is a file generated by FlutterFire CLI, it contains, the production app credentials, this is the only file I forgot to change when I was doing the dev environment testing, I forgot to create new apps for development and change the values in firebase_options.dart

Now what's happening is, the cloud function is generating the token correctly but whenever I try to sign in via this token in Flutter

await FirebaseAuth.instance.signInWithCustomToken(firebase_token);

I get this error

flutter (14645): Authentication error [firebase_auth/custom-token- 
mismatch] The custom token corresponds to a different audience.

How to fix this error? I can't just create new Firebase apps, if that's the root cause of the error, for two reasons

  1. I can't delete the existing because existing users won't be able to use the application

  2. I can't create new apps, because the Android package name is required, which is used by the existing apps.

So I want a solution that doesn't require disturbing existing users, if possible.

答案1

得分: 0

问题通过运行 Flutter cleanFlutter pub get 解决。

原来是 Flutter 在构建之间缓存了 Google 服务帐户。卸载应用程序并清除应用程序的缓存并不能解决问题,问题出在 Flutter 缓存了文件在电脑上。

我会回答自己的问题而不是删除这个帖子,因为这个答案可能会帮助未来的用户。关于这个问题的其他答案说要创建一个新的应用程序或创建一个新的密钥或其他什么。

如果你面临这个错误,而且没有任何方法对你有用,那就清除电脑上的缓存。

英文:

Issue solved by running Flutter clean, and Flutter pub get.

It turns out that Flutter caches The Google service account between builds. Uninstalling the app and clearing the cache of the app, doesn't help, it's Flutter that's caching the file on the PC.

I'm answering my own question rather than deleting this post because this answer might help future users. The other answers on this topic say to create a new app or create a new key or whatever.

If you face this error, and nothing works for you, then just clear the cache on your PC.

huangapple
  • 本文由 发表于 2023年4月17日 00:39:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定