SimpleAsPi
SimpleAsPi

Reputation: 11

Creating Firestore document with text field data

So im making a sign up page on xcode with firebase and it currently work well but I would like to get more information from the user such as the first name and the last name . Basically I want my code to automatically create a document named with the email of the user that just sign up in the "Users" collection on firestore and after create the field "FirstName" and "LastName" with the text in those textfield! You can see my code below . Thanks for your help in advance. And I also provide a screenshot ( I did it manually to explain what I want it to do )

@IBOutlet weak var FirstName: UITextField!
@IBOutlet weak var LastName: UITextField!
@IBOutlet weak var Email: UITextField!
@IBOutlet weak var Password: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

@IBAction func SignUpButton(_ sender: Any) {
    if Email.text?.isEmpty == true {
        print("No text in email field")
        return
    }
    if Password.text?.isEmpty == true {
        print("No text in password field")
        return
    }
    if FirstName.text?.isEmpty == true {
        print("No text in first name field")
        return
    }
    if LastName.text?.isEmpty == true {
        print("No text in Last name field")
        return

    }
    SignUp()
}

@IBAction func LoginButton(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(identifier: "SignInPage")
    vc.modalPresentationStyle = .overFullScreen
    present(vc, animated: true)
}

func SignUp() {

    Auth.auth().createUser(withEmail: Email.text!, password: Password.text!) { (authResult, error) in
        guard let user = authResult?.user, error == nil else {
            print("Error \(error!.localizedDescription)")
        return
        }

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(identifier: "Manager0")
           vc.modalPresentationStyle = .overFullScreen
        self.present(vc, animated: true)

    }
}

enter image description here

Upvotes: 0

Views: 468

Answers (1)

frunkad
frunkad

Reputation: 2543

You cannot get a user's first and last name as Firebase only honours the full name only via displayName.

Moreover, with email and password login, you only get the email - as Firebase would have no way to get the user's Name. (Suppose a user has Yahoo Email ID - how would it get the name of the user?)

Upvotes: 2

Related Questions