Ah yeap, sorry! Probably easier if i started that way too. Thank you!
//
// SignUpViewController.swift
// LogInTesting_HBK
//
// Created by Neal Revane on 2/8/22.
//
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 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(firstNameTextField)
Utilities.styleTextField(lastNameTextField)
Utilities.styleTextField(emailTextField)
Utilities.styleTextField(passwordTextField)
Utilities.styleFilledButton(signUpButton)
}
// Check the fields and validate that the data is correct. If everything is correct, this method returns nil. Otherwise, t returns the error message
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 required fields."
}
//Check if the password is secure
let cleanedPassword = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
if Utilities.isPasswordValid(cleanedPassword) == false {
//Password isnt secure enough
return "Please make sure your password is at least 8 characters, contains a capitalized letter and special character"
}
return nil
}
func signUpTapped(_ sender: Any){
// validate the fields
let error = validateFields()
if error != nil {
//There something wrong with the field show error message
showError(message: error!)
}
else {
// Create clean 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 user
self.showError(message: "Error Creating the 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 {
}
}
//transition to the home screen
self.transitionToHome()
}
}
}
func showError ( message:String) {
errorLabel.text = message
errorLabel.alpha = 1
}
func transitionToHome() {
let homeViewController = storyboard?.instantiateViewController(withIdentifier: Constants.Storyboard.homeViewController) as? HomeViewController
view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()
}
}