1

I try to do firebase authenticate. I've installed all necessary pods and made all needed methods. When I click "Login" even when I entered bad email and password, I get error message (e.g. bad password) and immediately my view is changed to my target view (next ViewController). I don't know why it is happening, because I made if statement. I want to have my login page unless I enter correct email and password.

 @IBAction func loginTapped(_ sender: UIButton) {
         if let email = emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), let password =
 passwordTextField.text?.trimmingCharacters(in:
 .whitespacesAndNewlines) {
             
            Auth.auth().signIn(withEmail: email, password: password) { (authResult, error) in
                if let e = error {
                     // Can't sign in
                     self.errorLabel.text = error!.localizedDescription
                     self.errorLabel.alpha = 1
                 } else {
                     self.performSegue(withIdentifier: "loginToHome", sender: self)
                }
             }
         }
     }
2
  • 1
    How you have added segue to move to next controller upon success ? Commented Feb 23, 2021 at 16:43
  • I have added it by mainstoryboard. I have dragged login button to new View and added segue "show" with identifier "loginToHome" Commented Feb 23, 2021 at 18:24

1 Answer 1

1

You may try to check if an error exists first. If yes, the error message will be shown. If there's no error, the login success segue will be performed.

@IBAction func loginTapped(_ sender: UIButton) {
    if let email = emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), let password = passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) {
        Auth.auth().signIn(withEmail: email, password: password) { (authResult, error) in
          if error != nil { // there's an error - show error label
               self.errorLabel.text = error!.localizedDescription
               self.errorLabel.alpha = 1
          } else { //no error - perform segue
           // no error - perform segue
          self.performSegue(withIdentifier: "loginToHome", sender: self) } } } }

Edit: You may also add return after checking that an error exists - the codes that follow after it will not be performed:

@IBAction func loginTapped(_ sender: UIButton) {
    if let email = emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), let password = passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) {
        Auth.auth().signIn(withEmail: email, password: password) { (authResult, error) in
          if error != nil { // there's an error - show error label
                self.errorLabel.text = error!.localizedDescription
                self.errorLabel.alpha = 1
                return // activities that follow will not be performed
          } else { //no error - perform segue
                self.performSegue(withIdentifier: "loginToHome", sender: self) } } } }
Sign up to request clarification or add additional context in comments.

10 Comments

Have you tried this code snippet before voting down? pls explain the downvote.
Ok, I've tried this, but it still doesn't work :(. It automatically moves to next view when I click login button
Maybe better option will be add instantiate view?
are you using the UIStoryboard or are you doing the views programmatically? If you are using the Storyboard, are you sure your UIButton is not connected to the next View?
If your segue is connected via the UIButton then it must be the problem...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.