使用电话号码的Firebase身份验证会返回内部错误 [英] Firebase Auth using phone number returns an internal error

查看:382
本文介绍了使用电话号码的Firebase身份验证会返回内部错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了我的应用程序,以便能够使用Firebase发送Apple通知,并验证了它可以使用控制台。现在我要做的是建立在APN之上的电话认证。



所以我写了这个:

<$ p如果错误!=零{
打印(验证码未发送\(错误!)$验证代码(phoneNumber){验证ID,
中的错误
> PhoneAuthProvider.provider ))
} else {
print(Successful。)
}

我得到:

$ p $ 错误域= FIRAuthErrorDomain代码= 17999发生内部错误,打印并检查错误的详细信息了解更多信息。 UserInfo = {NSUnderlyingError = 0x170046db0 {Error Domain = FIRAuthInternalErrorDomain Code = 3(null)UserInfo = {FIRAuthErrorUserInfoDeserializedResponseKey = {
code = 500;
message =< null>;
}}},error_name = ERROR_INTERNAL_ERROR,NSLocalizedDescription =发生内部错误,打印并检查错误的详细信息以获取更多信息。 b
$ b

有什么想法吗?我应该提交一个针对firebase的错误吗?

我使用的是iOS SDK 4.0.0(最新的zip可以找到。)


$ b $通过添加 FirebaseAppDelegateProxyEnabled

code> info.plist 并设置为 NO

  func application(_ application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data){
//将设备令牌传递给auth。
Auth.auth()。setAPNSToken(deviceToken,type:.prod)
}


解决方案

使用最新的 Firebase iOS SDK ie 4.0.0 Xcode 8.3 测试

首先,从info.plist中删除 FirebaseAppDelegateProxyEnabled 这个键。



现在在 AppDelegate.swift 中添加以下功能:

  import Firebase 
import UserNotifications

@UIApplicationMain
$ b class AppDelegate:UIResponder,UIApplicationDelegate,UNUserNotificationCenterDelegate {
var window :UIWindow?
func application(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?) - > Bool {

if #available(iOS 10.0,*){
//对于iOS 10显示通知(通过APNS发送)
UNUserNotificationCenter.current()。delegate = self
let authOptions:UNAuthorizationOptions = [.alert,.badge,.sound]
UNUserNotificationCenter.current()。requestAuthorization(
options:authOptions,
completionHandler:{_,_in} )
} else {
let settings:UIUserNotificationSettings =
UIUserNotificationSettings(类型:[.alert,.badge,.sound],类别:nil)
application.registerUserNotificationSettings(settings)

$ b application.registerForRemoteNotifications()
FirebaseApp.configure()
return true

func application(_ application:UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data){
//传递设备令牌授权
let firebaseAuth = Auth.auth()

//在开发时使用.sandbox
firebaseAuth.setAPNSToken(deviceToken,类型:AuthAPNSTokenType.sandbox)

//在生产时它将被设置为.prod


func应用程序(_ application:UIApplication,didReceiveRemoteNotification userInfo:[AnyHashable:Any],fetchCompletionHandler completionHandler:@ (UIBackgroundFetchResult) - > Void){
let firebaseAuth = Auth.auth()

if(firebaseAuth.canHandleNotification(userInfo)){
print(userInfo)
返回
}
} *

发送验证码给用户的电话: b
$ b
在你想要整合电话认证的类中写:

注意:我已将 +91 作为印度的国家代码。您可以根据您的地区添加国家代码。

  PhoneAuthProvider.provider()。verifyPhoneNumber(+ 919876543210){ ,错误)
if((error)!= nil){
//验证码未发送。
打印(错误)
} else {
//成功。用户获取验证码
//将验证码保存在UserDefaults
UserDefaults.standard.set(verificationID,forKey:firebase_verification)
UserDefaults.standard.synchronize()
//显示屏幕输入代码。

使用验证码登入使用者:

  let verificationID = UserDefaults.standard.value(forKey:firebase_verification)
let credential = PhoneAuthProvider.provider ).credential(withVerificationID:verificationID!as!String,verificationCode:self.txtEmailID.text!)

Auth.auth()。signIn(with:credential,completion:{(_ user:User,如果错误!= nil {
//错误
} else {
print(Phone number:\(user。 phoneNumber))
var userInfo:Any?= user.providerData [0]
print(userInfo)
}
} as!AuthResultCallback)


I set up my app to be able to send Apple Notifications using firebase and I verified that it works using the console. Now I want to do phone authentication which is built on top of APN.

So I wrote this:

PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber) { verificationID, error in
  if error != nil {
    print("Verification code not sent \(error!)")
  } else {
    print ("Successful.")
  }

And I get:

Error Domain=FIRAuthErrorDomain Code=17999 "An internal error has occurred, print and inspect the error details for more information." UserInfo={NSUnderlyingError=0x170046db0 {Error Domain=FIRAuthInternalErrorDomain Code=3 "(null)" UserInfo={FIRAuthErrorUserInfoDeserializedResponseKey={
    code = 500;
    message = "<null>";
}}}, error_name=ERROR_INTERNAL_ERROR, NSLocalizedDescription=An internal error has occurred, print and inspect the error details for more information.}

Any idea? Should I file a bug against firebase?

I am using iOS SDK 4.0.0 (latest zip I could find.)

UPDATE:

I disabled method swizzling by adding FirebaseAppDelegateProxyEnabled to info.plist and set it to NO

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Pass device token to auth.
    Auth.auth().setAPNSToken(deviceToken, type: .prod)
}

解决方案

Tested with latest Firebase iOS SDK i.e. 4.0.0 and Xcode 8.3

Firstly , remove this key FirebaseAppDelegateProxyEnabled from info.plist. This is not needed.

Now in AppDelegate.swift add following functions

import Firebase
import UserNotifications

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate , UNUserNotificationCenterDelegate{
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()
        FirebaseApp.configure()
        return true
    }
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Pass device token to auth.
    let firebaseAuth = Auth.auth()

    //At development time we use .sandbox
    firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)

    //At time of production it will be set to .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
    }
}*

Send a verification code to the user's phone:

In the class where you want to integrate Phone Authentication write :

Note : I have added +91 as its country code for India. You can add country code according to your region.

 PhoneAuthProvider.provider().verifyPhoneNumber("+919876543210") { (verificationID, error) in
       if ((error) != nil) {
             // Verification code not sent.
             print(error)
       } else {
              // Successful. User gets verification code 
              // Save verificationID in UserDefaults
             UserDefaults.standard.set(verificationID, forKey: "firebase_verification")
             UserDefaults.standard.synchronize()
             //And show the Screen to enter the Code.
       }               

Sign in the user with the verification code:

 let verificationID = UserDefaults.standard.value(forKey: "firebase_verification")
 let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationID! as! String, verificationCode: self.txtEmailID.text!)

   Auth.auth().signIn(with: credential, completion: {(_ user: User, _ error: Error?) -> Void in
         if error != nil {
            // Error
          }else {
             print("Phone number: \(user.phoneNumber)")
              var userInfo: Any? = user.providerData[0]
                    print(userInfo)
                }
         } as! AuthResultCallback)

这篇关于使用电话号码的Firebase身份验证会返回内部错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆