38

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

KENdi
  • 7,576
  • 2
  • 16
  • 31
Jordan Tate
  • 381
  • 1
  • 3
  • 7
  • You should provide a proper code example of what you are trying to do. – Fabien Jul 13 '17 at 21:59
  • thanks for the tip. i updated my post. – Jordan Tate Jul 13 '17 at 23:37
  • 1
    I have answered here : https://stackoverflow.com/questions/44091666/firebase-auth-using-phone-number-returns-an-internal-error/44241143#44241143 – Arpit Jain Jul 14 '17 at 10:32
  • 1
    Possible duplicate of [Firebase Auth using phone number returns an internal error](https://stackoverflow.com/questions/44091666/firebase-auth-using-phone-number-returns-an-internal-error) – Ajay S Jul 14 '17 at 19:27
  • '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? – Jay Jul 14 '17 at 19:27
  • For me it was that I had not correctly uploaded the APN key to Firebase – Guig Sep 21 '17 at 20:08
  • How did you fix it @JordanTate ? – Fakhruddin Abdi Aug 25 '18 at 07:00

10 Answers10

33

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.

algrid
  • 5,600
  • 3
  • 34
  • 37
30

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
Jayb967
  • 316
  • 3
  • 5
  • It worked for me. No need to build. It can be worked in all the release build also. – Balaji Ks Nov 13 '21 at 09:54
  • very well explained. Just a repeat, make sure to add following in appdelegate override init() { FirebaseApp.configure() } – embarker Jun 15 '22 at 10:02
8

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)
Arun Kumar P
  • 820
  • 2
  • 12
  • 25
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

Parth
  • 634
  • 8
  • 15
5

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.

Ignacio Valdivieso
  • 1,181
  • 8
  • 5
  • This was my issue. I delete the /ios folder for some reason and after that I forgot to set the correct bundle id – M. A. Jan 28 '22 at 15:09
3

For any one else who double checked the above and still have issues, don't forget to add your team ID and App Id in your ios config file.

And, don't forget to download it again, and replace your old one with the updated one.

enter image description here

Ruan
  • 3,969
  • 10
  • 60
  • 87
  • 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. – Michał Dziwota Apr 14 '22 at 12:14
2

Make sure that it is APNs that you are selected under Key Services. The number of APNs certificates per developer account is limited to 2. So if you already had 2 certificates before, there is a chance that you are created the certificate by checking DeviceCheck instead of APNs.

enter image description here

Naval Hasan
  • 1,226
  • 1
  • 13
  • 18
2

I faced same problem when I want send OTP and get notifications, I created APN key on apple developer console then added it to firebase after that everything got well:enter image description here

1

If you have tried adding the APNs key (p8) in your Firebase console and also checked for the Correct bundle Id and Correct Google-Info.plist. And still the issue persists Do check if you have "Push Notifications" enabled and "Remote notifications" selected in "Signing and capabilities" in Xcode->Runner but are not actually using firebase messaging Then care to uncheck "Remote notifications" from "Signing and capabilities" and remove "Push Notifications" in Xcode->Runner. In my case this was the issue

Dharman
  • 30,962
  • 25
  • 85
  • 135
kk4
  • 119
  • 4
0

If you did everything right as per the documentation. Try one thing.

Paste this line in AppDelegate.m under,

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

 [[FIRAuth auth] setAPNSToken:deviceToken type:FIRAuthAPNSTokenTypeProd];
Usama Shahid
  • 769
  • 6
  • 9