For my iOS app, I am currently checking if a user is logged in with email and password via firebase in my initial view controller, using the recommended method from the firebase documentation. If a user is not logged in, I then present my login screen as shown below:
class InitialViewController: UIViewController {
var authHandle: FIRAuthStateDidChangeListenerHandle!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
hasUserSignedIn()
}
func hasUserSignedIn() {
authHandle = FIRAuth.auth()?.addStateDidChangeListener { [unowned self] auth, user in
if user == nil {
self.perform(#selector(self.presentLogInSignUp), with: nil, afterDelay: 0)
}
}
}
However, when a user is not signed in, they briefly see my initial view controller before the log in controller is presented, which isn't a great user experience. A way to solve this would be to add a check in the app delegate's didFinishLaunchingWithOptions.
Firstly, is the app delegate and didFinishLaunchingWithOptions an OK place to check if a user is logged in with Firebase (would be done after FIRAppConfigure())?. I assume it is, as from my understanding, a user's Firebase logged in state is persisted in the keychain (is that correct?)
Secondly, if the app delegate is an appropriate place for checking a user's logged in state, should I use the recommended way as I do in my initial view controller or the method below? The advantage of latter is that I don't need to worry about having to remove a listener but Firebase docs gives a warning with this method that the auth object may not have finished initialising.
if FIRAuth.auth()?.currentUser != nil {
// User is signed in.
// ...
} else {
// No user is signed in.
// ...
}
Current potential solution: I use the recommended method in didFinishLaunchingWithOptions and I just remove the handler after using it within didFinishLaunchingWithOptions.
Firebase is awesome and look forward to implementing this correctly.