将Firebase身份验证与Firebase数据库连接

时间:2019-02-04 11:45:49

标签: swift firebase firebase-realtime-database firebase-authentication

我正在努力将iOS应用上的Firebase身份验证与Firebase数据库连接起来。

用户已成功通过身份验证,并显示在Firebase的“身份验证”部分中,但是数据(例如名称/电子邮件)未显示在应存储它的Firebase数据库中。我在下面粘贴了代码和调试区域的警告。

代码:

 @objc func handleRegister() {

    guard let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text
        else {
            print("form is not valid")
            return
    }

    Auth.auth().createUser(withEmail: email, password: password, completion: { (res, err) in

        if let err = err {
            print(err)
            return
        }


        self.ref = Database.database().reference()
        let userId = Auth.auth().currentUser?.uid
        self.ref.child("users").child(userId!).setValue(["username": name, "email": email]) {
            (error:Error?, ref:DatabaseReference) in
            if let error = error {
                print("Data could not be saved: \(error).")
            } else {
                print("Data saved successfully!")
            }
        }

    })
}
  

调试区域警告:

     

2019-02-06 19:29:06.735883 + 0100 MyAwesomeProject [72154:5241459]   libMobileGestalt MobileGestalt.c:890:MGIsDeviceOneOfType不是   在该平台上受支持。

     

2019-02-06 19:29:06.874146 + 0100 MyAwesomeProject [72154:5241580] 5.16.0   -[Firebase / Core] [I-COR000022] Firebase Analytics不可用。要添加它,请在Podfile中包含Firebase / Core或添加   FirebaseAnalytics.framework进入链接构建阶段

     

2019-02-06 19:29:08.015632 + 0100 MyAwesomeProject [72154:5241459]   [framework] CUIThemeStore:未注册ID = 0的主题

     

2019-02-06 19:29:09.123610 + 0100 MyAwesomeProject [72154:5241459] [MC]   systemgroup.com.apple.configurationprofiles的系统组容器   路径是   /Users/mikolajsemeniuk/Library/Developer/CoreSimulator/Devices/20AB5C8F-2A8E-4F49-8DA6-0BB11A11958F/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles

     

2019-02-06 19:29:09.125330 + 0100 MyAwesomeProject [72154:5241459] [MC]   从私人有效用户设置中读取。

     

2019-02-06 19:29:19.325723 + 0100 MyAwesomeProject [72154:5241579]   [自动填充]无法显示应用程序bundleID的自动强密码:   -234567.MyAwesomeProject由于错误:iCloud钥匙串已禁用2019-02-06 19:29:23.627072 + 0100 MyAwesomeProject [72154:5242009]   [BoringSSL] nw_protocol_boringssl_get_output_frames(1301)   [C1.1:2] [0x7fa94a80b030]获取输出帧失败,状态8196

     

2019-02-06 19:29:23.627344 + 0100 MyAwesomeProject [72154:5242009]   [BoringSSL] nw_protocol_boringssl_get_output_frames(1301)   [C1.1:2] [0x7fa94a80b030]获取输出帧失败,状态8196

     

2019-02-06 19:29:23.628087 + 0100 MyAwesomeProject [72154:5242009] TIC   读取状态[1:0x0]:1:57

     

2019-02-06 19:29:23.628233 + 0100 MyAwesomeProject [72154:5242009] TIC   读取状态[1:0x0]:1:57

     

2019-02-06 19:29:23.950084 + 0100 MyAwesomeProject [72154:5241765]   [BoringSSL] nw_protocol_boringssl_get_output_frames(1301)   [C2.1:2] [0x7fa948614e70]获取输出帧失败,状态8196

     

2019-02-06 19:29:23.950399 + 0100 MyAwesomeProject [72154:5241765]   [BoringSSL] nw_protocol_boringssl_get_output_frames(1301)   [C2.1:2] [0x7fa948614e70]获取输出帧失败,状态8196

     

2019-02-06 19:29:23.951137 + 0100 MyAwesomeProject [72154:5241765] TIC   读取状态[2:0x0]:1:57

     

2019-02-06 19:29:23.951409 + 0100 MyAwesomeProject [72154:5241765] TIC   读取状态[2:0x0]:1:57(lldb)

1 个答案:

答案 0 :(得分:0)

首先,您的属性列表文件plist决定要使用的Firebase项目,因此无需指定以下内容:

let ref = Database.database().reference(fromURL:"https://myawesomeproject29133.firebaseio.com/")

相反,您可以在ref函数上方创建一个名为viewDidLoad()的全局变量:

var ref: DatabaseReference!
override func viewDidLoad() {
        super.viewDidLoad()

}

在您的handleUser()中:

ref = Database.database().reference()
let userid =  Auth.auth().currentUser.uid
self.ref.child("users").child(userid).setValue(["name": name,"email": email])
  

此外,我强烈建议您继续操作而不保存他人的密码,除非您对他们进行加密。您不应该赤裸裸地存放它们。它存储在Google的服务器中,您绝对没有理由查看他人密码。