'How to retrieve and show user data from Firebase with swift?

I followed a tutorial to create a register and login screen with firebase and is already working, but now I want to create the screen "My account" with text fields where show the name, mail, etc.. of the user, so my question and I need help is how to get the data from firebase and show that information on that screen, how can I do it please?

I already read the firebase documentation https://firebase.google.com/docs/auth/ios/manage-users but I don't know how to proceed because Im learning programming/swift and I have to still improving my level

I attach the code of the sign up view

 import UIKit
 import FirebaseAuth
 import FirebaseFirestore
 import Firebase

 class SignUpViewController: UIViewController {

@IBOutlet weak var nameField: UITextField!

@IBOutlet weak var lastNameField: UITextField!

@IBOutlet weak var emailField: UITextField!

@IBOutlet weak var passwordField: UITextField!

@IBOutlet weak var signUpButton: UIButton!

@IBOutlet weak var errorLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    setUpElements()
}

func setUpElements(){
    
    //Hide the error label
    errorLabel.alpha = 0
    
    //Style the elements
    Utilities.styleTextField(nameField)
    Utilities.styleTextField(lastNameField)
    Utilities.styleTextField(emailField)
    Utilities.styleTextField(passwordField)
    Utilities.styleFilledButton(signUpButton)
    
}

// Check the fields and validate that the data is correct. If everything is correct, this method returns nil. Otherwise, it returns the error message
func validateFields() -> String? {
    
    // Check that all fields are filled in
    if nameField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
        lastNameField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
        emailField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
        passwordField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
        
        return "Please fill in all fields."
    }
    
    // Check if the password is secure
    let cleanedPassword = passwordField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
    
    if Utilities.isPasswordValid(cleanedPassword) == false {
        // Password isn't secure enough
        return "Please make sure your password is at least 8 characters, contains a special character and a number."
    }
    
    return nil
}


@IBAction func signUpTapped(_ sender: Any) {
    
    // Validate the fields
    let error = validateFields()
    
    if error != nil {
        
        // There's something wrong with the fields, show error message
        showError(error!)
    }
    else {
        
        // Create cleaned versions of the data
        let firstName = nameField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let lastName = lastNameField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let email = emailField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let password = passwordField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        
        // Create the user
        Auth.auth().createUser(withEmail: email, password: password) { (result, err) in
            
            // Check for errors
            if err != nil {
                
                // There was an error creating the user
                self.showError("Error creating user")
            }
            else {
                
                // User was created successfully, now store the first name and last name
                let db = Firestore.firestore()
                
                db.collection("users").addDocument(data: ["firstname":firstName, "lastname":lastName, "uid": result!.user.uid ]) { (error) in
                    
                    if error != nil {
                        // Show error message
                        self.showError("Error saving user data")
                    }
                }
                
                // Transition to the home screen
                self.transitionToHome()
            }
            
        }
        
        
        
    }
}

func showError(_ message:String) {
    
    errorLabel.text = message
    errorLabel.alpha = 1
}

func transitionToHome() {
    
    let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
    
    view.window?.rootViewController = homeViewController
    view.window?.makeKeyAndVisible()
    
}

}

And the actual code of my account view controller, just I add it the text fields, I don't know how to proceed

import UIKit

class MyAccountViewController: UIViewController {

@IBOutlet weak var nameField: UITextField!

@IBOutlet weak var lastnameField: UITextField!

@IBOutlet weak var emailField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

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

}

I think that I provided all the info necessary, thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source