Search code examples
swiftparse-platform

Password reset not checking for existing users


In my password reset function the user can put in whatever he/she wants. It does not even needs to be a email address.

I would like to check for a valid email address, and that the email is registered in Parse Server

@IBAction func forgotPasswordButtonTapped(_ sender: Any) {

let forgotPasswordAlert = UIAlertController(title: "Forgot password?", message: "Please enter your email address", preferredStyle: .alert)
forgotPasswordAlert.view.tintColor = UIColor.red
forgotPasswordAlert.addTextField { (textField) in
    textField.placeholder = "Email address"
}
forgotPasswordAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
forgotPasswordAlert.addAction(UIAlertAction(title: "Reset password", style: .default, handler: { (action) in
    let resetEmail = forgotPasswordAlert.textFields?.first?.text
    PFUser.requestPasswordResetForEmail(inBackground: resetEmail!, block: { (success, error) in
        if error != nil {
            let resetFailedAlert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
            resetFailedAlert.view.tintColor = UIColor.red
            resetFailedAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(resetFailedAlert, animated: true, completion: nil)
        } else {
            let resetEmailSentAlert = UIAlertController(title: "Password reset instructions sendt", message: "Please check your email", preferredStyle: .alert)
            resetEmailSentAlert.view.tintColor = UIColor.red
            resetEmailSentAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(resetEmailSentAlert, animated: true, completion: nil)
        }
    })

}))
//PRESENT ALERT
self.present(forgotPasswordAlert, animated: true, completion: nil)

}


Solution

  • In your action you can check for email validity:-

    forgotPasswordAlert.addAction(UIAlertAction(title: "Reset password", style: .default, handler: { (action) in
        let resetEmail = forgotPasswordAlert.textFields?.first?.text
    if self.isValidEmail(testStr: resetEmail) {
    // Check of email registered on server should be done via this API and API should return error based on that.
        PFUser.requestPasswordResetForEmail(inBackground: resetEmail!, block: { (success, error) in
            if error != nil {
                let resetFailedAlert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
                resetFailedAlert.view.tintColor = UIColor.red
                resetFailedAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                self.present(resetFailedAlert, animated: true, completion: nil)
            } else {
                let resetEmailSentAlert = UIAlertController(title: "Password reset instructions sendt", message: "Please check your email", preferredStyle: .alert)
                resetEmailSentAlert.view.tintColor = UIColor.red
                resetEmailSentAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                self.present(resetEmailSentAlert, animated: true, completion: nil)
            }
        })
    } else {
    //Show error that email entered is not correct format
    // present the reset email alertbox again.
    }
    
    }))
    
    func isValidEmail(testStr:String) -> Bool {        
        let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
    
        let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
        return emailTest.evaluate(with: testStr)
    }