简体   繁体   中英

FireFlutter: Enable App Check in debug mode

Problem

App Check works fine in production mode, but in debug mode I get errors:

401: Firebase App Check token is invalid.

I tried two things:

  • Generate a debug token using Firebase console.
  • Generate a debug token in the App via inserting <script>self.FIREBASE_APPCHECK_DEBUG_TOKEN = true;</script> into index.html . And then adding that to in the Firebase console as a debug token. I also noticed that this way a new debug token is generated on each App restart.

Code

  if (kReleaseMode) {
    await FirebaseAppCheck.instance.activate(
      webRecaptchaSiteKey: LIVE_TOKEN,
    );
  } else {
    await FirebaseAppCheck.instance.activate(
      webRecaptchaSiteKey: DEBUG_TOKEN,
    );
  }

Question

Using FlutterFire, what is the correct way to generate and use a debug token for App Check? Following the docs did not work for me.

Docs

https://firebase.google.com/docs/app-check/flutter/default-providers

https://firebase.flutter.dev/docs/app-check/debug-provider/#activating-the-debug-provider-web

I managed to get it working, following the next steps. I also reported this at FlutterFire Github .

  1. Add the following to the index.html
<body>
    <script>self.FIREBASE_APPCHECK_DEBUG_TOKEN = true;</script>
    <script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-app-check.js"></script>
    ......
  1. Run the app in debug mode, a debug token will be printed in the logs

App Check debug token: fb1a8616-b721-42c6-841c-544x5743ea72. You will need to add it to your app's App Check settings in the Firebase console for it to work.

  1. Add that debug token to App Check in the Firebase console

    https://firebase.google.com/docs/app-check/web/debug-provide

  2. Set the debug token in the dart code and restart (do not close the app)

const debugToken = 'fb1a8616-b721-42c6-841c-544x5743ea72';

await FirebaseAppCheck.instance.activate(
   webRecaptchaSiteKey: kReleaseMode ? liveToken : debugToken,
);

The main inconvenience is that each time the app is executed, a new debug token is generated and it must be set in the Firebase console, in the dart code and restart the app without closing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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