Firebase Auth创建用户

时间:2017-02-02 03:43:03

标签: ios swift firebase firebase-authentication

我似乎无法让这个工作。数据库部分工作,我在数据库中获取用户信息,但它不是在Firebase Auth中创建用户。对于以下代码,它打印“无法注册。”

有人可以告诉我我做错了吗?

import UIKit
import Firebase
import FirebaseAuth

class AddUserTableViewController: UITableViewController, UITextFieldDelegate {


@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!


@IBAction func saveUserButton(_ sender: Any) {
    let ref: FIRDatabaseReference!
    ref = FIRDatabase.database().reference()
    FIRAuth.auth()?.createUser(withEmail: emailTextField.text!, password: "pass", completion: { (user, error) in
        if error != nil {
            print ("Can't Register")
        }
        else {
            print ("I don't know what this means")
        }
    })

    ref?.child("Users").childByAutoId().setValue(["First Name": self.firstNameTextField.text, "Email": self.emailTextField.text])

}

1 个答案:

答案 0 :(得分:0)

只需加入Firebase,您也不需要在每个页面上都包含FirebaseAuth。

这是我的FireBase登录工作代码,我几周​​前从Youtube教程中做到了这一点。

import UIKit
import Firebase

class LoginController: UIViewController {
    @IBOutlet weak var menuButton:UIBarButtonItem!
    @IBOutlet weak var signinSelector: UISegmentedControl!
    @IBOutlet weak var signinLabel: UILabel!
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var signinButton: UIButton!

    var isSignIn:Bool = true


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func signinSelectorChanged(_ sender: UISegmentedControl) {
        //Flip the boolean true to false
        isSignIn = !isSignIn

        //Check the boolean and set the buttons and labels
        if isSignIn {
                signinLabel.text = "Sign In"
                signinButton.setTitle("Sign In", for: .normal)
        }
        else {
                signinLabel.text = "Register"
                signinButton.setTitle("Register", for: .normal)
        }
    }

    @IBAction func signinButtonTapped(_ sender: UIButton) {

        //Do some form validation on email and password
        if let email = emailTextField.text, let pass = passwordTextField.text
        {

        //Check if it's signed or register

        if isSignIn {
            //Sign in the user with Firebase
            Auth.auth().signIn(withEmail: email, password: pass, completion: { (user, error) in

                //Check that user isn't nil
                if let u = user {
                        //User is found, goto home screen
                    self.performSegue(withIdentifier: "goToHome", sender: self)
                }
                else{
                    //Error:  Check error and show message
                }
            })
        }

        else {
            //Register the user with Firebase

            Auth.auth().createUser(withEmail: email, password: pass, completion: { (user, error) in

                //Check that user isn't NIL
                if let u = user {
                    //User is found, goto home screen
                    self.performSegue(withIdentifier: "goToHome", sender: self)
                }
                else {
                    //Check error and show message
                }

            })



            }
    }


}

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //Dismiss the keyboard when the view is tapped on
        emailTextField.resignFirstResponder()
        passwordTextField.resignFirstResponder()
    }


}