So my goal is to validate a textfield by checking if that value is within any of the documents in the Firestore collection. So in my other validation function, I can return a String and show an alert with the error like so:
func validateFields() -> String? {
if nameTextF.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
emailTextF.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
passwordTextF.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
schoolIDTextF.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
currentGradeTextF.text?.trimmingCharacters(in: .whitespacesAndNewlines) == ""{
showAlert(title: "Missing Fields", message: "Please fill in all fields.")
return "Issue With Fields"
}
let cleanedPassword = passwordTextF.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let properGradeSelected = currentGradeTextF.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let validSchoolID = schoolIDTextF.text!.trimmingCharacters(in: .whitespacesAndNewlines)
if Utilities.isPasswordValid(cleanedPassword) == false {
showAlert(title: "Invalid Password", message: "Please ensure that your password contains 8 characters, contains a special character and a number as well.")
return "Issue With Password"
}
if Utilities.isGradeValid(properGradeSelected) == false {
showAlert(title: "Invalid Grade", message: "Please ensure that your current grade is valid.")
return "Issue With Grade Input"
}
if Utilities.isSchoolIDValid(validSchoolID) == false {
showAlert(title: "Invalid School ID Format", message: "The School ID entered has the incorrect format.")
return "Issue With School ID input."
}
return nil
}
Then I call it when the 'Sign Up' button is pressed like so:
#IBAction func signupPressed(_ sender: UIButton) {
//Validate the fields
let validationError = validateFields()
if validationError != nil {
return
} else {
//Create the user
Auth.auth().createUser(withEmail: email, password: password) { (result, err) in ....
This works perfect. Now since I want to use a Firestore query in a function, I can't return the String like how I did in the other ones without getting errors, so I'm quite confused on how to go about doing this.
This is the function I have so far:
func determineIfIDIsValid() {
let schoolIDText = schoolIDTextF.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
db.collection("school_users").whereField("school_id", isEqualTo: schoolIDText).getDocuments { (querySnapshot, error) in
guard error == nil else {
return
}
guard let query = querySnapshot?.isEmpty else { return }
if query == true {
//Show error alert
} else {
return
//Continue With Signup flow
}
}
}
I've tried declaring a variable before the query whether it be a String or Bool, changing the value after the query, and using logic to return a String but that didn't work either. How can I use this function to validate the specific field without getting any errors?
func determineIfIDIsValid(_ callback: #escaping (Bool) -> ()) {
let schoolIDText = schoolIDTextF.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
db.collection("school_users").whereField("school_id", isEqualTo: schoolIDText).getDocuments { (querySnapshot, error) in
guard error == nil else {
return
}
guard let query = querySnapshot?.isEmpty else { return }
if query == true {
//Show error alert
callback(true)
} else {
callback(false)
//Continue With Signup flow
}
}
}
Firebase calls are async. You need use closure
determineIfIDIsValid() { res in
if res {
//Show alert
} else {
//Continue With Signup flow
}
Related
I recently added
let username = usernameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let db = Firestore.firestore()
db.collection("users").whereField("username", isEqualTo: username).getDocuments { (snapshot, error) in
if error == nil && snapshot?.documents != nil {
}
return "Username has already been taken"
}
to a validatefields() function, placed in a Register View Controller.
Since adding it, the line:
return "Username has already been taken"
is throwing an "Unexpected non-void return value in void function"
The entire function is:
func validateFields() -> String? {
//Check everything is filled in
if usernameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" || 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 password is secure
let cleanedPassword = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
if Utilities.isPasswordValid(cleanedPassword) == false {
//Password isn't secure enough
return "Passwords must contain a minimum of 8 characters, using at least one capital, number and special character (!, ?, #, & etc.)"
}
let username = usernameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let db = Firestore.firestore()
db.collection("users").whereField("username", isEqualTo: username).getDocuments { (snapshot, error) in
if error == nil && snapshot?.documents != nil {
}
return "Username has already been taken"
}
return nil
}
Any help would be massively appreciated.
I am writing some custom store methods for in app purchases; sort of a wrapper for SwiftyStore. The problem I'm running into is the inability to get the results from the closures before they exit.
Any suggestions on how to properly set them up? IE: Closures...
I have a function that checks for an existing subscription and returns true if it finds one in firebase, if it doesn't then it goes out to the apple store to verify a previously purchased subscription:
func checkSubscription() -> Bool {
var RetVal: Bool = false
var retStat: String = ""
var myVal: Bool = false
self.rootRef.child("users").child(self.userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let mySubType = value?["subtyp"] as? String ?? ""
// set value
if mySubType == "" {
// get receipt
if self.myStore.getReceipt() == true {
(myVal, retStat) = self.myStore.verifyPurchase(product: "com.xxxxx.xxxxx.monthly")
if myVal == true && retStat == "Valid" {
// we have a valid product update firebase
print("Valid")
} else if myVal == true && retStat == "Expired" {
// we have a valid product that is expired
print("Expired")
}
}
} else {
// we have a purchase, verify its not expired.
print("Purchased")
RetVal = true
}
}) { (error) in
print(error.localizedDescription)
}
return RetVal
}
The problem here is its dropping down to the return RetVal before the closure is complete so the function could be returning an invalid value. Not sure how I can fix this in the current setup, but any suggestions or pointers would be appreciated.
To expand on Tom's comment, if you want to return a result when the nested asynchronous function is complete, you could pass in a completion handler closure that uses the Result type that Swift offers like the following:
func checkSubscription(completion: #escaping (Result<Bool, Error>) -> Void) {
var RetVal: Bool = false
var retStat: String = ""
var myVal: Bool = false
self.rootRef.child("users").child(self.userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let mySubType = value?["subtyp"] as? String ?? ""
// set value
if mySubType == "" {
// get receipt
if self.myStore.getReceipt() == true {
(myVal, retStat) = self.myStore.verifyPurchase(product: "com.xxxxx.xxxxx.monthly")
if myVal == true && retStat == "Valid" {
// we have a valid product update firebase
print("Valid")
} else if myVal == true && retStat == "Expired" {
// we have a valid product that is expired
print("Expired")
}
}
} else {
// we have a purchase, verify its not expired.
print("Purchased")
RetVal = true
}
completion(.success(RetVal))
}) { (error) in
print(error.localizedDescription)
completion(.failure(error))
}
}
Calling the function using this type of completion handler would look something like this:
checkSubscription { (result) in
switch result {
case .success(let boolValue):
// do something with resulting boolean
break
case .failure(let error):
// do something with resulting error
break
}
}
func checkSubscription(completion: (_ scuess:Bool) ->()){
var RetVal: Bool = false
var retStat: String = ""
var myVal: Bool = false
self.rootRef.child("users").child(self.userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let mySubType = value?["subtyp"] as? String ?? ""
// set value
if mySubType == "" {
// get receipt
if self.myStore.getReceipt() == true {
(myVal, retStat) = self.myStore.verifyPurchase(product: "com.xxxxx.xxxxx.monthly")
if myVal == true && retStat == "Valid" {
// we have a valid product update firebase
print("Valid")
} else if myVal == true && retStat == "Expired" {
// we have a valid product that is expired
print("Expired")
}
}
completion(false)
} else {
// we have a purchase, verify its not expired.
print("Purchased")
completion(true)
}
}) { (error) in
print(error.localizedDescription)
completion(false)
}
return RetVal
}
call completion(true) whenever your retValue supposed to be true and completion(false) whenever your retValue supposed to be true
Then call this function this way:
checkSubscription { (sucuess) in
if(sucuess){
print("OK")
}else{
print("BAD")
}
}
I am trying to implement a login through Parse, however, I am having a number of problems.
The first two If statements work but then perform a segue back to the first screen, even though there is no segue on my storyboard that even does this. So why is the alert popping up doing this? It doesn't wait or let me press OK.
When I sign up a user, it registers them (given the condition is met) to Parse but doesn't perform the segue which I have implemented? Fyi it doesn't show the alert error either.
On parse, there are frequently users signed up with random letters. I don't know how or why this happens.
Thank you in advance.
Jake
if passwordField.text == "" || confirmPasswordField.text == "" || usernameField == "" || lastNameField.text == "" || firstNameField.text == "" {
doAlert(title: "Incomplete Form", message: "You have not filled in all the forms")
} else if passwordField.text != confirmPasswordField.text {
doAlert(title: "Password mismatch", message: "Your passwords do not match")
} else {
let user = PFUser()
user.username = usernameField.text
user.password = passwordField.text
user["First Name"] = firstNameField.text
user["Last Name"] = lastNameField.text
user.signUpInBackground(block: { (success, error) in
if error != nil {
var displayMessage = "Please try again later"
if let errorMessage = error?.userInfo["error"] as? String {
displayMessage = errorMessage
} } else {
self.performSegue(withIdentifier: "RegisteredViewController", sender: self)
}
})
}
I am working on a function that handles user registration and in the process, check if the selected username entered by the user is taken or not to inform the user to select a different one. I have the below code to accomplish this scenario:
#IBAction func proceedPressed(sender: AnyObject) {
/**********************Perform Validation************************/
if(self.emailTxtField.text != "" && self.passwordTxtField.text != "")
{
print("Email and Password not empty")
self.usernameValidation({(result) -> Void in
if(result == false)
{
print("Result False")
self.usernameErrorLabel.text = "Username Taken"
}else{
print("Result True")
//Username is available...Proceed
self.usernameErrorLabel.text = ""
FIRAuth.auth()?.createUserWithEmail(self.emailTxtField.text!, password: self.passwordTxtField.text!) { (user, error) in
if(error == nil)
{
print("Creating User with Email")
/*Create the user object as submitted*/
self.dbReference.child("users").child(user!.uid).setValue(["username": self.emailTxtField.text!,"name":self.nameTxtField.text!, "email":self.emailTxtField.text!, "mobile":self.mobileTxtField.text!, "homeAddress":"N", "workAddress":"N", "otherAddress":"N", "profilePictureRef":"N","telephone":"0","friendsCount":0, "retailersCount":0])
}else{
print("Error occured: \(error?.description)")
}
}//end of createUserWithEmail
}
})
}else{
print("Error: Email or Password field is empty")
}
}
and to check the username:
func usernameValidation(completion: (result: Bool) -> Void)
{
print("Username is: \(self.usernameTxtField.text!)")
dbReference.child("usernamesTaken").queryOrderedByValue().queryEqualToValue(self.usernameTxtField.text!).observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot!) -> Void in
print(snapshot.childrenCount)
if(snapshot.childrenCount == 0)
{
print("result is true in username validation")
//Username Available
completion(result:true)
}else{
print("result is false in username validation")
//Username Taken
completion(result:false)
}
})
}
The problem with the above is that the full code doesn't seem to execute. When button pressed, I get the following messages in console:
- Email and Password not empty
- Username is: [value entered in usernameTxtField.text
and then nothing more. Although I wrote many print statements to try and see where this is stopping, but this is the furthest the code went in terms of printing the statements.
Is there something wrong here that I am missing out?
Thanks in advance.
I did some more testing and then discovered the issue through the xcode console. I copied the following from the firebase website to test fetching the data:
ref.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
let username = snapshot.value!["username"] as! String
let user = User.init(username: username)
// ...
}) { (error) in
print(error.localizedDescription)
}
The above showed an error that is "Permission Denied". Following that I edited the Rules in the database section in the console and allowed .read and .write and that did it. I thought I would post the details just in case someone else gets stuck.
Instead to write a big "if" statement, I want to know if this syntax is ok ?
I just use the word return if the condition is not respected.
#IBAction func SignUpAction(sender: AnyObject) {
if passwordField1.text != passwordField2.text {
// password dont match
// Pop error
return
}
let user = PFUser()
user.username = emailField.text
user.password = passwordField1.text
user.signUpInBackgroundWithBlock { (suceeded: Bool, error: NSError?) -> Void in
if error == nil {
// Log the user
}else{
// Display an error
}
}
}
Or I need to write it like this :
#IBAction func SignUpAction(sender: AnyObject) {
if passwordField1.text == passwordField2.text {
let user = PFUser()
user.username = emailField.text
user.password = passwordField1.text
user.signUpInBackgroundWithBlock { (suceeded: Bool, error: NSError?) -> Void in
if error == nil {
// Log the user
}else{
// Display an error
}
}
}else{
// Error : password dont match
// Pop error
}
}
the first syntax is more clear if need to try many conditions but I'm not sure it's good to write the code like that. Thanks
SOLUTION : (with matt answer and Apple Doc on "guard" :
The first statement need to be write like this :
guard passwordField1.text == passwordField2.text else {
print("pop error")
return
}
Actually, I'd use the first syntax, expressed as a guard statement:
guard passwordField1.text == passwordField2.text else { return }
This is exactly what guard is for: if conditions are not met, go no further. A succession of multiple guard statements (because multiple conditions must be met) is quite standard.