如何拦截Firebase登录事件"失败:permission_denied"

时间:2017-12-09 11:56:48

标签: swift firebase firebase-authentication

我在我的应用中使用firebase进行谷歌登录。 规则仅允许登录我的公司域。 当我使用@ mydomain.com邮件时,用户正确登录。 当我尝试使用其他邮件登录时,我在控制台中只有这样的消息:

[Firebase/Database][-----] Listener at /user_profiles/... failed: permission_denied.

但应用程序仍保留在登录视图控制器上,没有任何内容。

有一种拦截此消息的方法,以便我可以在屏幕上显示警告吗?

my code in app delegate didFinishLaunchingWithOptions

这不是登录错误,因为gmail登录和firebase登录工作正常,并且永远不会调用错误。 由于我对公司域的限制,错误是写入/读取错误。 我在哪里可以处理读/写错误?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
    GIDSignIn.sharedInstance().delegate = self
    return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    return GIDSignIn.sharedInstance().handle(url,
                                             sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
                                             annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}

//let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main", bundle: nil)




func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {

    if let error = error {
        print("error:")
        print(error.localizedDescription) // never called

        return
    }
    print("Gmail login - OK")

    let authentication = user.authentication
    let credential = GoogleAuthProvider.credential(withIDToken: (authentication?.idToken)!,accessToken: (authentication?.accessToken)!)

    Auth.auth().signIn(with: credential) { (user, error) in

        if let error = error {
            print("error:")
            print(error.localizedDescription) // never called
            return
        }
        print("Firebase login - OK")

        self.databaseRef = Database.database().reference()
        self.databaseRef.child("user_profiles").child(user!.uid).observeSingleEvent(of: .value, with: { (snapshot) in

            let snapshot = snapshot.value as? NSDictionary

            if(snapshot == nil) {
                self.databaseRef.child("user_profiles").child(user!.uid).child("name").setValue(user?.displayName)
                self.databaseRef.child("user_profiles").child(user!.uid).child("email").setValue(user?.email)
            }


            self.window?.rootViewController?.performSegue(withIdentifier: "HomeViewSegue", sender: nil)
        })
    }
}

1 个答案:

答案 0 :(得分:-1)

解决!我添加了这张支票:

            self.databaseRef.observe(.childAdded, with: { data in }, withCancel: { (returnedError: Error) in

            let testError: NSError = returnedError as NSError

            print("Firebase error:\(testError)")

        })