iosflutterfirebase-authentication

invalid-app-credential/Token mismatch in Flutter firebase_auth


I've started to setup Firebase Phone authentication in Flutter based on the official documentation. Unfortunately, no matter how I try, I get a FirebaseAuthException in the verificationFailed callback of the verifyPhoneNumber method with these values:

code: "invalid-app-credential"
credential: null
email: null
message: "Token mismatch"
phoneNumber: null
plugin: "firebase_auth"
stackTrace: null
tenantId: null

I've created a simple repository to reproduce the issue: https://github.com/peternagy1332/basic_phone_auth

  1. I used flutterfire configure to add the app to an existing Firebase project and create the IOS application there.
  2. I've added firebase_core@2.1.1 and firebase_auth@4.1.1
  3. I've enabled the Phone sign-in method on Firebase and added +44 7123 123 456 as a test number, and the code is 000000
  4. I've added the Push notification and Background modes capability with Background fetch and Remote notifications options.
  5. I've added the the REVERSED_CLIENT_ID from GoogleService-Info.plist as an URL Scheme.
  6. On the Apple Developer page, I've generated a new key with Apple Push Notifications service (APNs) service and uploaded it as an APNs Authentication Key to Firebase.

The setup section of the official documentation points me to this documentation. This indicates that additional modifications in the Swift code might be needed, but it's really unclear. I'm a Flutter developer and not a native IOS developer, I don't think I actually need to do all that.


Solution

    1. Make sure you set FirebaseApp.configure() in AppDelegate.swift
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
        ) -> Bool {
            FirebaseApp.configure() 
            GeneratedPluginRegistrant.register(with: self)
            return super.application(application, didFinishLaunchingWithOptions: launchOptions)
        }
    
    1. Set the APNS token type to AuthAPNSTokenType.unknown. The actual token type will be detected from the provisioning profile in the app’s bundle.
        override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            let firebaseAuth = Auth.auth()
            firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.unknown)
        }
    
    1. Change the aps-environment value from "development" to "unknown" in your Runner.entitlements file

    Runner.entitlements