"Invalid Token" when trying to authenticate phone number using firebase

28,610

Solution 1

I was also experiencing this problem. Checked the following:

  • Correct bundle Id
  • Correct Google-Info.plist
  • Correct aps-environment value
  • Correct APNS token type when calling auth.setAPNStoken (.unknown for auto detect)

Nothing helped until in Firebase app settings I uploaded the APNS authentication key (p8) instead of certificates - I used those certificates before for push notifications only and everything was working fine but for phone number notifications something went wrong.

Solution 2

It is most likely that you need to upload an .p8 Key file (I have an Enterprise account but same thing for developer)
In Apple Developer Account:

  • All Keys
  • Create New Key (+) enter image description here
  • Type in Global name for all of your apps
  • Checkbox next to Apple Push Notifications service (APNs) enter image description here
  • Download your p8 file
  • Upload to firebase dashboard enter image description here

Solution 3

First regenerates APNS key and upload in firebase for cloud messaging

1) Import Firebase and FirebaseAuth

import Firebase
import FirebaseAuth

2) In didFinishLaunchingWithOptions Configure firebase.

FirebaseApp.configure()

3) Write these two func in AppDelegate.

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let firebaseAuth = Auth.auth()
    firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod)

}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    let firebaseAuth = Auth.auth()
    if (firebaseAuth.canHandleNotification(userInfo)){
        print(userInfo)
        return
    }
}

Very Important note : uthAPNSTokenType set correctly for [sandbox / production] or set for common .unknown

In my case it was the apns token type that was wrong:

Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod)

should have been:

Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)

Solution 4

In my case, the problem was an incorrect bundle id in the iOS app, in the firebase project settings (Project settings -> General -> Your apps).

I hope this helps anyone overlooking the same detail.

Solution 5

same problem questions have been before on SO. so would like to tell you setup all pre-require step before run code.

Pre-Require Steps:

  • Register bundle id on Developer account and enable notifications for bundle id.

  • Register same bundle id on firebase console setting page and create app, download Google-Info.plist file, make sure name should be same.

  • Upload Push certificates on firebase console for sandbox as well as development.

  • follow this below link for code implementation.

setup code for Firebase Auth

Share:
28,610

Related videos on Youtube

Jordan Tate
Author by

Jordan Tate

Updated on January 29, 2022

Comments

  • Jordan Tate
    Jordan Tate over 2 years

    This is my code:

    import FirebaseAuth
    
    
    class AuthPhoneNum {
    
        static func getPhoneNum(phoneNumber: String) {
            PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber) { (verificationID, error) in
                if let error = error {
                    print(error)
                    return
                }
                UserDefaults.standard.set(verificationID, forKey: "authVerificationID")
            }
        }
    
        static func verify(verificationCode: String?) {
            guard let verificationID = UserDefaults.standard.string(forKey: "authVerificationID") else { return }
            if verificationCode != nil {
                let credential = PhoneAuthProvider.provider().credential(
                    withVerificationID: verificationID,
                    verificationCode: verificationCode!)
    
                Auth.auth().signIn(with: credential) { (user, error) in
                    if let error = error {
                        print(error)
                        return
                    }
                }
            } else {
                print("No verification code")
            }
        }
    
    }
    

    This is what the console prints out:

    Error Domain=FIRAuthErrorDomain Code=17048 "Invalid token." UserInfo={NSLocalizedDescription=Invalid token., error_name=INVALID_APP_CREDENTIAL}

    What am I doing wrong? Thanks

    • Fabien
      Fabien almost 7 years
      You should provide a proper code example of what you are trying to do.
    • Jordan Tate
      Jordan Tate almost 7 years
      thanks for the tip. i updated my post.
    • Arpit Jain
      Arpit Jain almost 7 years
    • Ajay S
      Ajay S almost 7 years
    • Jay
      Jay almost 7 years
      'Also, note that phone number sign-in requires a physical device and won't work on a simulator.' If that's not the issue, did you enable phone number authentication in your console?
    • Guig
      Guig over 6 years
      For me it was that I had not correctly uploaded the APN key to Firebase
    • Fakhruddin Abdi
      Fakhruddin Abdi over 5 years
      How did you fix it @JordanTate ?
  • Anil Gupta
    Anil Gupta over 6 years
    I was also missing the APNs .p12 file in Firebase console. Once I Uploaded the .p12 then it worked.
  • Fakhruddin Abdi
    Fakhruddin Abdi over 5 years
    Do you mean .p8 @AnilGupta, because i couldn't find adding .p12 APN anywhere
  • Fakhruddin Abdi
    Fakhruddin Abdi over 5 years
    @algrid Where aps-environment ?
  • Dilip Tiwari
    Dilip Tiwari over 5 years
    hiii i need help
  • Famic Tech
    Famic Tech over 5 years
    Where do you upload the p12?!
  • maxhawkins
    maxhawkins over 4 years
    When I deleted my APNS key and re-uploaded it it worked. 🤬
  • Mohit Kumar
    Mohit Kumar over 3 years
    I was not allowed to replace APNS Certificate.But changing to sandbox worked for me.Thank you so much
  • Govind Maheshwari
    Govind Maheshwari over 3 years
    i am face same issue in react native ios release build...after adding prod code in appdelegate.m its worked. thanks buddy
  • Balaji Ks
    Balaji Ks over 2 years
    It worked for me. No need to build. It can be worked in all the release build also.
  • M. A.
    M. A. over 2 years
    This was my issue. I delete the /ios folder for some reason and after that I forgot to set the correct bundle id
  • tate_xy
    tate_xy about 2 years
    Worked Like a charm
  • matsbauer
    matsbauer about 2 years
    i really tried a lot, but the sandbox type worked, finally! Thanks
  • Michał Dziwota
    Michał Dziwota about 2 years
    Thanks! In my case, it was an incorrect Team ID. Perhaps App Store ID is not necessarily - I have it blank and it works anyway.