Taras
Taras

Reputation: 1889

Verification ID used to create the phone auth credential is invalid iOS

I can't login and always see this error message:

The verification ID used to create the phone auth credential is invalid

I tried to add my phone like tester, but I wasn't succeed on it. Sign in method is turn on in Firebase console.

I found the same issue here, but without answer. I used firebase's docs. Help me please to figure it out.

Here my code:

func verifyPhone(_ phone: String) -> AsyncTask<String?> {
    return AsyncTask<String?>({ observer, lifetime in
        guard !lifetime.hasEnded else {
            observer.sendInterrupted()
            return
        }
        Auth.auth().languageCode = "ua"
        PhoneAuthProvider.provider().verifyPhoneNumber(phone, uiDelegate: nil) { verificationID, error in
            if let error = error {
                observer.send(error: AppError(error))
                return
            }
            observer.send(value: verificationID)
            observer.sendCompleted()
        }
    })
}

func signInViaPhoneNumber(usingCode smsCode: String) -> AsyncTask<Void> {
    return AsyncTask<Void>({ observer, lifetime in
        guard !lifetime.hasEnded else {
            observer.sendInterrupted()
            return
        }
        guard let verificationCode = UserDefaultsStorage.verificationCode else {
            observer.send(error: AppError.logic("Відсутній код верифікації"))
            return
        }
        
        let credential = PhoneAuthProvider.provider().credential(withVerificationID: smsCode,
                                                                 verificationCode: verificationCode)
        Auth.auth().signIn(with: credential) { result, error in
            if let error = error {
                observer.send(error: AppError(error))
                return
            }
            
            guard let firUser = result?.user else {
                observer.send(error: AppError.logic("Відсутній юзер"))
                return
            }
            let factory: ModelFactory = ModelFactoryImpl()
            let appUser = factory.makeUser(id: firUser.uid, name: firUser.displayName ?? "", phone: firUser.phoneNumber ?? "")
            AppService.shared.user = appUser
            observer.send(value: ())
            observer.sendCompleted()
        }
    })
}

Upvotes: 1

Views: 1695

Answers (1)

Razvan S.
Razvan S.

Reputation: 803

It's hard for me to see the complete code flow because you are using AsyncTask and delegating all the results outside. The issue that pops out in your code is the line

PhoneAuthProvider.provider().credential(withVerificationID: smsCode,
                                                                 verificationCode: verificationCode)

In this method verificationCode should be the code you received via SMS and withVerificationID should be the verificationId that you got in the verifyPhoneNumber callback.

Upvotes: 1

Related Questions