title: how to properly use addStateDidChangeListener in Firebase Authentication?
body: <p>I am trying to create registerVC, after the user fill the text field, and press the register button I try to create user by using Firebase Authentication, </p>
  
  <pre><code>Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
    // ...
  }
  </code></pre>
  
  <p>here is the code when the sign up button did pressed</p>
  
  <pre><code>@IBAction func signUpButtonDidPressed(_ sender: Any) {
          print("000")
          SVProgressHUD.show(withStatus: "Harap Tunggu")
  
          print("a")
  
          userEmail = emailTextField.text!
          userPassword = passwordTextField.text!
  
          print("b")
  
          Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in
              print("c")
  
  
              print("d")
  
  
  
              if let user = user {
                  print("e")
  
                  user.sendEmailVerification(completion: { (error) in
                      print("f")
                      print("g")
  
  
                      let fullName = "\(self.firstNameTextField.text!) \(self.lastNameTextField.text!)"
                      let emailOfTheUser = "\(self.emailTextField.text!)"
  
                      print("h")
  
                      let userKMViaEmail = UserKM(uid: user.uid, fullname: fullName, email: emailOfTheUser )
  
                      print("i")
  
  
                      print("111")
  
                      // save user basic information in NSUserDefault
                      self.saveUserKMBasicDataInUserDefault(userKM: userKMViaEmail)
                       print("2222")
  
                      self.showAlert(alertTitle: "BErhasil", alertMessage: "email dikirim", actionTitle: "OK")
  
                      // the user need to read the alert first, after tap the action the user will be moved to LoginUsingEmailVC
  //                    self.showCustomAlert(alertTitle: "Periksa Email Anda!", alertMessage: "Assalamualaikum, Mohon Ikuti link yang ada pada email yang kami kirimkan. Harap tunggu 15-30 menit apabila email kami belum juga sampai.", actionTitle: "Login")
  //
                       print("3333")
                      SVProgressHUD.dismiss()
  
                       print("444")
  
                  })
              }
  
          }
  
  
  
  
      }
  </code></pre>
  
  <p>In app delegate, I write codes to decide whether the user will go straight to Main Tab Bar if the user has already sign in, otherwise the user will be sent to Welcome Screen VC when the app is launched.</p>
  
  <p>here is the code in my App Delegate.</p>
  
  <pre><code>@UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate {
  
      var window: UIWindow?
  
  
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {
  
          FirebaseApp.configure()
          checkUserLogin()
  
  
          // to track the user default info.plist location in the device (simulator) for checking purpose.
          print("The Location of info.plist of User Default: \(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)")
  
  
          return true
      }
  
  }
  
  
  
  
  extension AppDelegate {
  
      // MARK: - HELPER METHODS
  
      func checkUserLogin() {
          // to check whether the user has already logged in or not
  
  
          SVProgressHUD.show()
          Auth.auth().addStateDidChangeListener { (auth, user) in
  
              if user == nil {
                  SVProgressHUD.dismiss()
                  self.goToWelcomeVC()
              } else {
                  SVProgressHUD.dismiss()
                  self.goToMainTabBar()
              }
  
              // if user is not nil then automatically go to maintab bar (initialVC is indeed main tab bar controller)
              SVProgressHUD.dismiss()
          }
      }
  
  
  
      func goToMainTabBar () {
           print("XXXXXXXX")
          let storyboard = UIStoryboard(name: "Main", bundle: nil)
          let mainTabBar = storyboard.instantiateViewController(withIdentifier: "mainTabBar")
          window?.rootViewController = mainTabBar
  
      }
  
      func goToWelcomeVC () {
          let storyboard = UIStoryboard(name: "Welcome", bundle: nil)
          let login = storyboard.instantiateViewController(withIdentifier: "WelcomeVC")
          window?.rootViewController = login
  
      }
  
  
  }
  </code></pre>
  
  <p>here is the print result in my debugging area, we can see that the XXXXX that located in AppDelegate is triggered automatically.</p>
  
  <p><a href="https://i.stack.imgur.com/W2zaK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W2zaK.png" alt="enter image description here"></a></p>
  
  <p>as we can see, after the sign up button did pressed <code>Auth.auth().createUser()</code> is triggered. and when <code>Auth.auth().createUser()</code> is triggered then <code>Auth.auth().addStateDidChangeListener</code> in AppDelegate will also be triggered.</p>
  
  <p>but unfortunately, when <code>Auth.auth().addStateDidChangeListener</code> in AppDelegate is triggered, it will make my app automatically go to Main Tab Bar.</p>
  
  <p>at this point, I want my keep stay on SignUpVC, not to MainTabBarVC.</p>
  
  <p>maybe I make mistake when writing the code below, because when createUser() is activated, basically the user in <code>addStateDidChangeListener()</code> is no longer nil.</p>
  
  <pre><code>Auth.auth().addStateDidChangeListener { (auth, user) in
  
              if user == nil {
                  SVProgressHUD.dismiss()
                  self.goToWelcomeVC()
              } else {
                  SVProgressHUD.dismiss()
                  self.goToMainTabBar()
              }
  
              // if user is not nil then automatically go to maintab bar (initialVC is indeed main tab bar controller)
              SVProgressHUD.dismiss()
          }
  </code></pre>
  
  <p>but I also want the user to be in MainTab bar if they already log in, otherwise go to welcome screen if they are not sign in yet?</p>