Home > database >  How to create a new user in firebase
How to create a new user in firebase

Time:10-17

I have been trying to follow the docs to authenticate a new user into firebase but when my user sign up their information is not appearing in firebase authentication

So I tried turning my password/email text field into a string that might help get rid of this issue but I received this error

Cannot convert value of type 'UITextField?' to expected argument type 'String'

so I tried using

let{

But now I can't type into the text field at all to enter the user data into firebase so how do I properly set up this code to authenticate my new user data ?

import UIKit
import FirebaseAuth
class SignUpViewController: UIViewController {
    @IBOutlet weak var Email: UITextField!
    
    @IBOutlet weak var Password: UITextField!
    @IBOutlet weak var Username: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
         let password  = Password.text
       let email = Email.text
         
               
        
        Auth.auth().createUser(withEmail: email!, password: password!) { authResult, error in
           
            if let error = error {
                print("Error \(error.localizedDescription)")
                                      return
              } else {
            }

CodePudding user response:

Try using a different name for the variables, possibly lowercased instead of

let Password = Password.text
let Email = Email.text

use

let password = Password.text
let email = Email.text

in case the compiler is complaining and using the TextField instead of the text contained in it, or if you are using SwiftUI just pass a binging like so:

@State private var email: String = ""
@State private var password: String = ""

TextField("Email here...", text: $email)
SecureField("Password here...", text: $password)

Button {
  // ... check if fields are not null
  Auth.auth().createUser(withEmail: email, password: password) { res, err in
    // ...
  }
} label: {
  Text("Create User")
}

EDIT 1:

Try this and make sure to create the right segues with right names if you are copy pasting and also that the storyboard components are correctly configured and allow write text.

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

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

@IBAction func createUser(_ sender: UIButton) {
    if let email = emailTextField.text, let password = 
passwordTextField.text {
        // create the user with email and password
    }
}
  • Related