EmailLoginViewController.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import UIKit
  15. import FirebaseAuth
  16. protocol EmailLoginDelegate {
  17. func emailLogin(_ controller: EmailLoginViewController, signedInAs user: User)
  18. func emailLogin(_ controller: EmailLoginViewController, failedWithError error: Error)
  19. }
  20. class EmailLoginViewController: UIViewController {
  21. // MARK: - Public Properties
  22. var delegate: EmailLoginDelegate?
  23. // MARK: - User Interface
  24. @IBOutlet private var emailAddress: UITextField!
  25. @IBOutlet private var password: UITextField!
  26. // MARK: - User Actions
  27. @IBAction func logInButtonHit(_ sender: UIButton) {
  28. guard let (email, password) = validatedInputs() else { return }
  29. Auth.auth().signIn(withEmail: email, password: password) { [unowned self] result, error in
  30. guard let result = result else {
  31. print("Error signing in: \(error!)")
  32. self.delegate?.emailLogin(self, failedWithError: error!)
  33. return
  34. }
  35. print("Signed in as user: \(result.user.uid)")
  36. self.delegate?.emailLogin(self, signedInAs: result.user)
  37. }
  38. }
  39. @IBAction func signUpButtonHit(_ sender: UIButton) {
  40. guard let (email, password) = validatedInputs() else { return }
  41. Auth.auth().createUser(withEmail: email, password: password) { [unowned self] result, error in
  42. guard let result = result else {
  43. print("Error signing up: \(error!)")
  44. self.delegate?.emailLogin(self, failedWithError: error!)
  45. return
  46. }
  47. print("Created new user: \(result.user.uid)!")
  48. self.delegate?.emailLogin(self, signedInAs: result.user)
  49. }
  50. }
  51. // MARK: - View Controller Lifecycle
  52. override func viewDidLoad() {}
  53. // MARK: - Helper Methods
  54. /// Validate the inputs for user email and password, returning the username and password if valid,
  55. /// otherwise nil.
  56. private func validatedInputs() -> (email: String, password: String)? {
  57. guard let userEmail = emailAddress.text, userEmail.count >= 6 else {
  58. presentError(with: "Email address isn't long enough.")
  59. return nil
  60. }
  61. guard let userPassword = password.text, userPassword.count >= 6 else {
  62. presentError(with: "Password is not long enough!")
  63. return nil
  64. }
  65. return (userEmail, userPassword)
  66. }
  67. func presentError(with text: String) {
  68. let alert = UIAlertController(title: "Error", message: text, preferredStyle: .alert)
  69. alert.addAction(UIAlertAction(title: "Okay", style: .default))
  70. present(alert, animated: true)
  71. }
  72. }