Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 [email protected] and [email protected]
  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.

like image 356
user1642349 Avatar asked Dec 22 '25 08:12

user1642349


2 Answers

  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

like image 70
Zander Avatar answered Dec 23 '25 21:12

Zander


In a Flutter application I was facing firebase_auth/invalid-app-credential: Invalid token error. The game changer for me was adding the APNs auth key to Firebase. In order to do that:

1 - Go to the Apple Developer website and create an Apple Push Notifications service (APNs) key. Then download it.

2 - In the Firebase > Project Settings > Cloud Messaging > Apple app configuration upload the APNs key.

like image 20
shuster Avatar answered Dec 23 '25 22:12

shuster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!