Manu Manu - 5 months ago 69
Swift Question

Register a new user on firebase

i am new working with firebase and i am making a login and register windows for an app using firebase.

Right now the log in worked fine for me as long as i created the account from the console
This is what the login code looks like

but then if you want to create a new account you click on that button down there and it will take you to a new viewcontroller window, there i added things to fill to make a new a account and i used

FIRAuth.auth()?.createUserWithEmail(<email: String>, password: <String>, completion: <FIRAuthResultCallback?(FIRUser?, NSError?) -> Void#>)


here a picture of how the code looks like
picture of the code and the imageview

When i try to register a new user i get the doesn't work message and i really don't know why it doesn't create a new user

bonus question
when i make it work how do i make it go from that window to another?

Answer

This should work:

var ref: FIRDatabaseReference!


override func viewDidLoad() {
    super.viewDidLoad()

    ref = FIRDatabase.database().reference()
    // Do any additional setup after loading the view.
}


@IBAction func signup(sender: AnyObject) {
    if let em = email.text, pass = password.text{
        FIRAuth.auth()?.createUserWithEmail(email.text!, password: password.text!) {(user, error) in
            if let error = error {
                print(error.localizedDescription)
            }
            else {
                print("User signed in!")

                self.ref.child("data/users").updateChildValues(["\(FIRAuth.auth()!.currentUser!.uid)":["Username":self.username.text!]])

                self.performSegueWithIdentifier("home", sender: self)
                //At this point, the user will be taken to the next screen
            }
        } }
    else{
        print("You left email/password empty")
    }
}

Make sure you've installed both Firebase, FirebaseAuth and FirebaseDatabase (If you're using cocoapods, your pod should like the one below)

pod 'Firebase'
pod 'Firebase/Auth'
pod 'Firebase/Database'