AlexFlutter
AlexFlutter

Reputation: 123

How to handle Firebase Appcheck Debug tokens and providers in flutter

As the docs are not clear about some things, I came to ask you guys here.

So, Appcheck has been a big pain for me to implement on flutter for 2 weeks now, and when using the Debug tokens in my app on ios or Android I end up with a lot of unexpected behavior.

here's an example from my Android & ios projects where I place the token providers note that I am using flutter:

main.dart

void main() async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();

  MobileAds.instance.initialize();

  // preserves splash screen
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);

  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

  await FirebaseAppCheck.instance.activate(
      webRecaptchaSiteKey: 'recaptcha-v3-site-key',
      androidProvider:
      kReleaseMode ? AndroidProvider.playIntegrity : AndroidProvider.debug,
      appleProvider: kReleaseMode ? AppleProvider.deviceCheck : AppleProvider.debug
  );
  ruApp(...);
}

Android MainActivity.kt

package com.baroraproject.app.barora
//
import android.os.Bundle
import com.google.firebase.FirebaseApp
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {

        if (BuildConfig.DEBUG) {
            FirebaseApp.initializeApp(/*context=*/this)
            val firebaseAppCheck = FirebaseAppCheck.getInstance()
            firebaseAppCheck.installAppCheckProviderFactory(
                    DebugAppCheckProviderFactory.getInstance()
            )
        }
        super.onCreate(savedInstanceState)
    }
}

IOS Runner/AppDelegeate.swift

import UIKit
import Flutter
import awesome_notifications
import shared_preferences_foundation
import FirebaseCore
import FirebaseAppCheck


@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
      
      // This function registers the desired plugins to be used within a notification background action
      SwiftAwesomeNotificationsPlugin.setPluginRegistrantCallback { registry in
          SwiftAwesomeNotificationsPlugin.register(
            with: registry.registrar(forPlugin: "io.flutter.plugins.awesomenotifications.AwesomeNotificationsPlugin")!)
          SharedPreferencesPlugin.register(
            with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
      }
      
      // Use the debug provider in Debug builds:
  #if DEBUG
      let providerFactory = AppCheckDebugProviderFactory()
      AppCheck.setAppCheckProviderFactory(providerFactory)
  #endif
      
      FirebaseApp.configure()
      GeneratedPluginRegistrant.register(with: self)
      
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

my questions are:
1- Should I Remove the debug provider code after I get the debug token logged and stored in Firebase?
2- When does the app check debug token refresh or change? and how do I prevent that from happening?
3- How should I go about keeping the app check debug provider codes outside of my release build? As the Debug statements don't work and when I release I would have to comment on the code that has to do with app check, clean, and rebuild.

Upvotes: 1

Views: 1260

Answers (2)

user3091464
user3091464

Reputation: 1

Once the token is found, add it here and your app won't have issues.

Manage Debug Tokens

Upvotes: 0

AlexFlutter
AlexFlutter

Reputation: 123

[SOLUTUION] Again, the dumb docs fo firebase caused it, so, you should NEVER edit on your main.kt or AppDelegate or whatever that has native code when using almost all services except for the configuration call in IOS:

  1. Delete any native app check code.
  2. Clean build
  3. Add the app check package to Flutter as needed.
  4. Run the app, notice the Log Cat for Android, and if the debug log is not printed in ios also try to get the XCode terminal involved.
  5. If no app check log is found, first turn off the app and start from cold (don't uninstall it).

Upvotes: 0

Related Questions