Search code examples
swiftfirebasefirebase-authenticationsegue

Is there a way to perform a conditional segue in a swift ViewController?


I am attempting to authenticate user sign in using Firebase. If a user's credentials are verified, I am seguing to the home screen of my application within an if-else statement using user and error. However, performSegue(...) executes whether or not the log in actually occurred.

I am certain that the problem is not with verification/login/logout issues in Firebase or with the textfields.

Here is my code:

func handleSignIn(username:String, password:String) {
    Auth.auth().signIn(withEmail: username, password: password) { user, error in
        if error == nil && user != nil {
            //self.dismiss(animated: false, completion: nil)
            print("Logged in!")
            self.performSegue(withIdentifier: "toHome", sender: nil)
        } else {
            print("Error logging in: \(error!.localizedDescription)")
        }
    }
}

Best,

Code Daddy


Solution

  • I have solved this issue.

    The code above is actually correct and functional. The issue was that the handleSignIn method was called within:

     @IBAction func didTapLogin(_ sender: Any) {...}
    

    That button itself was the segue connection with the identifier "toHome," and so regardless of what the if/else block evaluated to, the application proceeded to the home menu.

    Once I deleted and reestablished the segue "toHome" from the LoginViewController to the HomeViewController (not the Log In button to the HomeViewController), this code worked properly.