I'm new to the swift world and I'm developing my application using Firebase features. I followed the procedures in the Firebase documentation and managed to implement the Authentication Controllers normally, except for one detail that I find strange. When the user is already authenticated, it displays the LoginViewController about half a second before going to the MainController. Is that correct? I would like the app to go directly to the MainController if the user is already logged in. Thank you.
Follow the code:
class LoginViewController: UIViewController {
@IBOutlet weak var textFieldPassword: UITextField!
@IBOutlet weak var textFieldEmail: UITextField!
var authStateListener: AuthStateDidChangeListenerHandle?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
authStateListener = Auth.auth().addStateDidChangeListener { (auth, user) in
if user != nil {
self.callMainController()
}
}
}
func callMainController() {
if let storyboard = self.storyboard {
let viewController = storyboard.instantiateViewController(withIdentifier: "MainController") as! UITabBarController
self.present(viewController, animated: true, completion: nil)
} else {
Alert(controller: self).show(message: "Error on show MainController")
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Auth.auth().removeStateDidChangeListener(authStateListener!)
}
@IBAction func didTapLogin(_ sender: Any) {
if let email = self.textFieldEmail.text, let password = self.textFieldPassword.text {
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
if let error = error {
print(error.localizedDescription)
return
}
print("User Logged")
}
}
}