title: Firebase automatic login on iOS
body: <p>Is there a better way to make a user automatically login to the app as long as they have logged in in the past; other than saving their login details directly to the storage which may be insecure?</p>
  
  <p>Thanks</p>
  
  <p>PS: I'm using Firebase and Swift for iOS</p>
  
  <p>EDIT: Here's the code</p>
  
  <pre><code>import UIKit
  import Firebase
  
  class loginVC: UIViewController {
  
      @IBOutlet weak var emailTextField: UITextField!
      @IBOutlet weak var passwordTextField: UITextField!
      @IBOutlet weak var errorLabel: UILabel!
  
      override func viewDidAppear(animated: Bool) {
          automaticLogin()
      }
  
      @IBAction func loginTapped(sender: AnyObject) {
          errorLabel.text = ""
          FIRAuth.auth()?.createUserWithEmail(emailTextField.text!, password: passwordTextField.text!, completion: {
              user, error in
              if error != nil {
                  FIRAuth.auth()?.signInWithEmail(self.emailTextField.text!, password: self.passwordTextField.text!, completion: { (user, error) in
                      if error == nil {
                          self.login()
                      } else if error != nil {
                          self.errorLabel.text = ("Invalid email address or password")
                          print(error)
                      }
                  })
              } else {
                  print("user created")
                  self.login()
                  print(user?.displayName)
              }
          })
      }
  
      @IBAction func forgotPasswordTapped(sender: AnyObject) {
          let email = emailTextField.text
          errorLabel.text = ""
  
          FIRAuth.auth()?.sendPasswordResetWithEmail(email!) { error in
              if error != nil {
                  self.errorLabel.text = ("Invalid email address")
                  print(error)
              } else {
                  self.errorLabel.text = ("Password reset email successfully sent")
                  print("password reset email sent")
              }
          }
      }
  
      func login() {
          FIRAuth.auth()?.signInWithEmail(emailTextField.text!, password: passwordTextField.text!, completion: {
              user, error in
              if error != nil {
                  self.errorLabel.text = ("Invalid email address or password")
                  print(error)
              } else {
                  print("login successful")
                  self.checkIfUserIsNew()
              }
          })
      }
  
      func checkIfUserIsNew() {
          if FIRAuth.auth()?.currentUser?.displayName != nil || FIRAuth.auth()?.currentUser?.displayName == "" {
              self.performSegueWithIdentifier("showChatVC", sender: self)
          } else {
              self.performSegueWithIdentifier("showOptionsVC", sender: self)
          }
      }
  
      func automaticLogin() {
          FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
              if user == user {
                  print("still signed in")
              } else {
                  print("not signed in")
              }
          }
      }
  </code></pre>
  
  <p>I also used this to logout the user:</p>
  
  <pre><code>    @IBAction func logOutTapped(sender: AnyObject) {
      try! FIRAuth.auth()?.signOut()
      performSegueWithIdentifier("showLoginVC", sender: self)
      print(FIRAuth.auth()?.currentUser?.displayName)
  }
  </code></pre>