I have an app set up where the user can log in and log out (this is handled via a Parse-server hosted by heroku, with a Parse framework).
To log in, the user inputs credentials, and if the server verifies them a segue moves them to another viewcontroller. Signing out signs the user out of the app and segues that back to the login screen. These work fine.
If the initial login credentials are incorrect, an alert is displayed prompting the user to retry and the user is not logged-in (obviously) and is left on the login screen.
A problem arises, though, if the user is signed-in, logs out, and then inputs incorrect credentials (in this order, with no closure of the simulator). The incorrect credentials bring up the prompting alert; hitting "OK" closes the alert, the user is not logged in, but the view is segued to the logout screen (where the sign out button is... this should only be accessible to logged in users). No segue segue exists (either coded or on the story board) that moves the user from the login directly to the logout screen.
No actions work since many require the users ID, and there is none since none is logged in. I have no idea why the incorrect login would move the VC's in that way since no segue exists that can do that. There is one that does that movement in reverse (when logging out), but none in that direction.
Anybody have any ideas? Below is the relevant logging-in code.
@IBAction func signInTapped(_ sender: UIButton) {
UIApplication.shared.beginIgnoringInteractionEvents()
PFUser.logInWithUsername(inBackground: userNameTextField.text!, password: passwordTextField.text!, block: { (user, error) in
if user != nil{
self.performSegue(withIdentifier: "signInSegue", sender: self)//if the sign in works
} else{
if let errorString = (error! as NSError).userInfo["error"] as? String{
let errorMessage = errorString
self.displayAlert("Failed login", message: errorMessage)//this is the code run when the login fails, yet it somehow can sometimes segue to the other viewcontroller
}
}
})
}
Here is the step by step schematic of what is going on.
- The user starts at the login page and inputs his/her credentials
- This segues the user (now logged on) to a
tabbarViewController
- They then navigate to another tab and there is a sign out button
- Pressing the sign out button causes an alert to appear, hitting yes segues the user (now logged out) to the original screen
- The user is now at the login screen. Inputting the wrong info brings up an alert
- The alert appears. Hitting yes closes the alert is supposed to leave the user on the login screen. However, there is an immediate segue to the logout screen again... this is neither coded nor manually set in the storyboard
Random things I think might be relevant:
There is an initial segue, called on the viewDidAppear
of the first VC (the login screen) upon turning on the app. It checks if there is a user logged in, via
if PFUser.current().username? != nil {
//perform segue
}
This segue brings them to the map section on the tab bar (NOT the sign out page as is happening with the issue).
EDIT: Upon the "segue" to the logout screen occurring, the login screen drops (like literally, falls downward on the screen until its not seen and the logout screen is the then viewable which is not the way any segues in the app function). Also, only the viewDidAppear
code is called when this transition occurs, NOT the viewDidLoad
code.
EDIT 2: I think the issue has to do with the view hierarchy. Here is the code for the login function:
PFUser.logInWithUsername(inBackground: userNameTextField.text!, password: passwordTextField.text!, block: { (user, error) in
if user?.username != nil{
self.performSegue(withIdentifier: "signInSegue", sender: self)
} else{ //if everything in this code block is commented, the issue does not occur
if let errorString = (error! as NSError).userInfo["error"] as? String{
let errorMessage = errorString
self.displayAlert("Failed login", message: errorMessage)
}
}
})
If I comment out everything in the else
part of the if statement, the issue does not occur. What I believe is happening is that the alert is displayed, and hitting "ok" closes the alert and sends the app back to the topmost (maybe I'm wrong with what I'm calling it) viewController, which is somehow the signout VC. I think a good way to fix it would be to add like a self.present(homescreenVC)
or something like this to the else
code, but I do not know how to do this.