I built a registration page in Swiftui with 8 text fields and connected it successfully with Firestore Firebase. The problem is, I have declared many conditionals for:
- The user must check the password strength before pressing the "Register Button".
- If one of the fields is empty, this will occur an error.
- The password and re-password must be matched.
- If nothing is wrong, the user should see that he/she successfully registered.
After registering, the fields should be empty.
The button declaration: Button(action: { alertPresented.toggle()
// //Text Fields
// firstName = ""
// lastName = ""
// email = ""
// password = ""
// phoneNum = ""
// studentID = ""
// coursesNum = ""
// repassword = ""
// //After checking the strength
// passwordStatus = ""
}) {
Text("Register")
}
.padding(12)
.frame(width: UIScreen.main.bounds.width - 150)
.background(Color("Color"))
.foregroundColor(.white)
.cornerRadius(8)
.alert(isPresented: $alertPresented, content: {
getAlerts()
})
}
The getAlerts function:
func getAlerts() -> Alert {
if self.password != self.repassword {
return Alert(title: Text("Error!"), message: Text("The passwords NOT matching"), dismissButton: .default(Text("OK!"))) }
if (self.firstName == "") && (self.lastName == "") && (self.email == "") && (self.password == "") && (self.repassword == "") && (self.studentID == "") && (self.phoneNum == "") && (self.coursesNum == "") {
return Alert(title: Text("Error!"), message: Text("Something MISSING!"), dismissButton: .default(Text("Got it!")))
}
if self.passwordStatus == "Weak" {
return Alert(title: Text("Error!"), message: Text("You can't register if the password is WEAK!"), dismissButton: .default(Text("Got it!"))) }
if trigger == 0 {
return Alert(title: Text("Error!"), message: Text("Please check the strength first"), dismissButton: .default(Text("Got it!")))
} else {
data.addData(firstName: firstName, lastName: lastName, email: email, password: password, phoneNum: phoneNum, studentID: studentID, coursesNum: coursesNum)
return Alert(title: Text("Congrats!!"), message: Text("You successfully REGISTERED, you can go back to Login"), dismissButton: .default(Text("Awesome!!")))
}
}
The problem is when everything is correct and I press the Register button, I got more than 5 documents in Firestore, and the "Something Missing" alert shows, and the fields went empty.