简体   繁体   English

“电话身份验证凭据是使用空验证 ID 创建的”- Firebase SwiftUI

[英]"The phone auth credential was created with an empty verification ID" - Firebase SwiftUI

I did phone authentication with firebase, but when I enter the verification code, I get an error "The phone auth credential was created with an empty verification ID"我用firebase做了电话认证,但是当我输入验证码时,我得到一个错误“The phone auth credential was created with an empty verification ID”

Screen: enter image description here屏幕:在此处输入图像描述

Code:代码:

import SwiftUI
import Firebase

class OTPViewModel: ObservableObject {
    
    @Published var number: String = ""
    @Published var code: String = ""
    
    @Published var otpText: String = ""
    @Published var otpFields: [String] = Array(repeating: "", count: 6)
    
    @Published var showAlert: Bool = false
    @Published var errorMsg: String = ""
    @Published var verificationCode: String = ""
    @Published var isLoading: Bool = false
    @Published var navigationTag: String?
    
    @AppStorage("log_status") var log_status = false
    
    func sendOTP()async{
        if isLoading{return}
        do{
            isLoading = true
            let result = try await
            PhoneAuthProvider.provider().verifyPhoneNumber("+\(code)\(number)", uiDelegate: nil)
            DispatchQueue.main.async {
                self.isLoading = false
                self.verificationCode = result
                self.navigationTag = "VERIFICATION"
            }
        }
        catch{
            handleError(error: error.localizedDescription)
        }
    }
    
    func handleError(error: String){
        DispatchQueue.main.async {
            self.isLoading = false
            self.errorMsg = error
            self.showAlert.toggle()
        }
    }
    
    func verifyOTP()async{
        do{
            isLoading = true
            let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationCode, verificationCode: otpText)
            let _ = try await Auth.auth().signIn(with: credential)
            DispatchQueue.main.async {[self] in
                isLoading = false
                log_status = true
            }
        }
        catch{
            handleError(error: error.localizedDescription)
        }
    }
}

I tried add this, before isLoading = true on the verifyOtp Method.我尝试在 verifyOtp 方法的 isLoading = true 之前添加这个。

otpText = otpFields.reduce("") { partialResult, value in
   partialResult + value
}

But still not worked但仍然没有奏效

Had the same issue, here's the solution:有同样的问题,这是解决方案:

  1. In your sendOTP() function after self.verificationCode = result add UserDefaults.standard.set(self.verificationCode, forKey: "authVerificationID")在你的sendOTP() function 之后self.verificationCode = result添加UserDefaults.standard.set(self.verificationCode, forKey: "authVerificationID")
  2. In your verifyOTP() function after isLoading = true add let verificationCode = UserDefaults.standard.string(forKey: "authVerificationID")?? ""isLoading = true之后的verifyOTP() function 添加let verificationCode = UserDefaults.standard.string(forKey: "authVerificationID")?? "" let verificationCode = UserDefaults.standard.string(forKey: "authVerificationID")?? ""

The point was that you needed to save your verification ID and then retrieve it to ensure that it is still valid if your app is terminated before the user completes the sign-in flow.关键是您需要保存您的验证 ID,然后检索它以确保如果您的应用程序在用户完成登录流程之前终止,它仍然有效。 See clause 3.3 of the documentation here .请参阅此处文档的第 3.3 条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM