When the user is already authenticated, it displays the LoginViewController about half a second before going to the MainController

Viewed 133

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")
        }
    }
}
2 Answers

You should check the login status in AppDelegate and set rootViewController in didFinishLaunchingWithOptions function

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        FirebaseApp.configure()

        var initialViewController: UIViewController!

        if Auth.auth().currentUser == nil {
          initialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainViewController")//replace your mainviewcontroller identifier
        } else {
          initialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController")//replace your loginviewcontroller identifier
        }

        window?.rootViewController = initialViewController
        window?.makeKeyAndVisible()

        return true
      }

I got it that way. Thanks a lot for the help.

    var initialViewController: UIViewController!

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    if Auth.auth().currentUser == nil {
        initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
    } else {
        initialViewController = storyboard.instantiateViewController(withIdentifier: "MainViewController")
    }

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
Related