Hi everyone,
So I’ve been trying to troubleshoot for days now but I can’t seem to find a solution and would love some help!
I originally followed Chris’ “Firebase Authentication Tutorial 2020 - Custom iOS Login Page” tutorial video and it was fantastic! Everything seems to work properly that was covered in the video.
Now, I have created a separate view controller that the user is brought to if they are signing up (NewUserHomeView Controller). This is a different view controller the user is brought to if they are just signing in (HomeViewController).
On the NewUserHomeViewController (and other view controllers later on) I want to be able to simply access and print out all of the data fields for that user. For now, this is just firstName, lastName, email, and password. Again, this project is essentially what Chris built in his Firebase iOS Login Page tutorial.
Currently, I have a segue that sends the string: (firstNameTextField.text! + " " + lastNameTextField.text!) to the NewUserHomeViewController. This should equal to the document ID for that user (I used .setData() to specify the document ID when a user is created). I am trying to use the string as the reference ID so that I can print out the data for that user. Hopefully that makes sense. Here is the code of the two view controllers:
SignUpViewController
…
import UIKit
import FirebaseAuth
import Firebase
import FirebaseFirestore
class SignUpViewController: UIViewController {
**@IBOutlet** **weak** **var** firstNameTextField: UITextField!
**@IBOutlet** **weak** **var** lastNameTextField: UITextField!
**@IBOutlet** **weak** **var** emailTextField: UITextField!=
**@IBOutlet** **weak** **var** passwordTextField: UITextField!
**@IBOutlet** **weak** **var** signUpButton: UIButton!
**@IBOutlet** **weak** **var** backButton: UIButton!
**@IBOutlet** **weak** **var** errorLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemTeal
// Do any additional setup after loading the view.
setUpElements()
}
func setUpElements() {
//hides error label
errorLabel.alpha = 0
//Style the elements
Utilities.styleTextField(firstNameTextField)
Utilities.styleTextField(lastNameTextField)
Utilities.styleTextField(emailTextField)
Utilities.styleTextField(passwordTextField)
Utilities.styleFilledButton(signUpButton)
}
func validateFields() -> String? {
// Check that all fields are filled in
if firstNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" || lastNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" || emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" || passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
return "Please fill in all fields."
}
//Check if this password is secure.
let cleanedPassword = passwordTextField.text!.trimmingCharacters(in:
.whitespacesAndNewlines)
if Utilities.isPasswordValid(cleanedPassword) == false {
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) {
self.performSegue(withIdentifier: "sourceVCtoDestinationVC", sender: self)
//Validate the fields
let error = validateFields()
if error != nil {
showError(error!)
}
else {
// Create cleaned versions of the data
let firstName = firstNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let lastName = lastNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let email = emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let password = passwordTextField.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 usr
self.showError("Error creating user")
}
else {
// User was created successfully, now store the firstname and lastname
let db = Firestore.firestore()
db.collection("users").document("\(firstName) \(lastName)").setData(["firstName":firstName, "lastName":lastName, "uid": result!.user.uid]) { (error) in
//check to see if saving is successful
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 newUserHomeViewController1 = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.newUserHomeViewController) as? NewUserHomeViewController
view.window?.rootViewController = newUserHomeViewController1
view.window?.makeKeyAndVisible()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "sourceVCtoDestinationVC") {
let destinationVC = segue.destination as! NewUserHomeViewController
destinationVC.name = firstNameTextField.text! + " " + lastNameTextField.text!
//destinationVC.name = firstName + lastName
}
}
}
…
NewUserHomeViewController
…
import UIKit
import Firebase
import FirebaseFirestore
import FirebaseFirestoreSwift
class NewUserHomeViewController: UIViewController {
**let** db = Firestore.firestore()
**var** name: String?
**@IBOutlet** **weak** **var** welcomeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let db = Firestore.firestore()
welcomeLabel.text = name
print(name!)
**let** docRef = db.collection("users").document(name!)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
print("Document data: \(dataDescription)")
} else {
print("Document does not exist")
}
}
}
}
…
My code keeps outputting “Document does not exist” when ran. Sorry for the lengthy post, but I would really appreciate any help! I must be using the wrong syntax or something…
Thanks in advance,
Matt