Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. //
  2. // MainViewController.swift
  3. // Witit-iOS
  4. //
  5. // Created by Nikolas Andryuschenko on 8/2/16.
  6. // Copyright © 2016 Witit Co. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Firebase
  11.  
  12. class MainViewController: UIViewController {
  13.  
  14. @IBOutlet weak var usernameLabel: UILabel!
  15. @IBOutlet weak var emailTextField: UITextField!
  16. @IBOutlet weak var passwordTextField: UITextField!
  17.  
  18. @IBOutlet weak var logoutButton: UIButton!
  19.  
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22.  
  23. if let user = FIRAuth.auth()?.currentUser {
  24.  
  25. self.logoutButton.alpha = 1.0
  26. self.usernameLabel.text = user.email
  27.  
  28.  
  29. } else {
  30.  
  31. self.logoutButton.alpha = 0.0
  32. self.usernameLabel.text = ""
  33.  
  34. }
  35.  
  36. // Do any additional setup after loading the view.
  37. }
  38.  
  39. override func didReceiveMemoryWarning() {
  40. super.didReceiveMemoryWarning()
  41. // Dispose of any resources that can be recreated.
  42. }
  43.  
  44. @IBAction func createAccountButtonTapped(sender: UIButton) {
  45.  
  46. if self.emailTextField.text == "" || self.passwordTextField.text == "" {
  47.  
  48. // put alert controllers in error handeling swift file
  49. let alertController = UIAlertController(title: "Error", message: "Please enter email and password.", preferredStyle: .Alert)
  50. let defaultAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
  51. alertController.addAction(defaultAction)
  52.  
  53. self.presentViewController(alertController, animated: true, completion: nil)
  54. } else {
  55.  
  56. FIRAuth.auth()?.createUserWithEmail(self.emailTextField.text!, password: self.passwordTextField.text!, completion: { (user, error) in
  57.  
  58. if error == nil {
  59.  
  60. self.logoutButton.alpha = 0
  61. self.usernameLabel.text = user!.email
  62. self.emailTextField.text = ""
  63. self.passwordTextField.text = ""
  64.  
  65. } else {
  66.  
  67. //Change location of alertcontroller
  68. let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .Alert)
  69. let defaultAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
  70. alertController.addAction(defaultAction)
  71.  
  72.  
  73. }
  74. })
  75.  
  76. }
  77.  
  78. }
  79.  
  80. @IBAction func loginButtonTapped(sender: UIButton) {
  81.  
  82. if self.emailTextField.text == "" || self.passwordTextField.text == "" {
  83.  
  84. // put alert controllers in error handeling swift file
  85. let alertController = UIAlertController(title: "Error", message: "Please enter email and password.", preferredStyle: .Alert)
  86. let defaultAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
  87. alertController.addAction(defaultAction)
  88.  
  89. self.presentViewController(alertController, animated: true, completion: nil)
  90. } else {
  91.  
  92. FIRAuth.auth()?.signInWithEmail(self.emailTextField.text!, password: self.emailTextField.text!, completion: { (user, error) in
  93.  
  94. if error == nil {
  95.  
  96. self.logoutButton.alpha = 0
  97. self.usernameLabel.text = user!.email
  98. self.emailTextField.text = ""
  99. self.passwordTextField.text = ""
  100.  
  101. } else {
  102.  
  103. //Change location of alertcontroller
  104. let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .Alert)
  105. let defaultAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
  106. alertController.addAction(defaultAction)
  107.  
  108.  
  109. }
  110.  
  111.  
  112. })
  113.  
  114. }
  115.  
  116.  
  117. }
  118.  
  119. @IBAction func logoutActionButtonTapped(sender: UIButton) {
  120.  
  121. try! FIRAuth.auth()?.signOut()
  122. self.usernameLabel.text = ""
  123. self.logoutButton.alpha = 0
  124. self.emailTextField.text = ""
  125. self.passwordTextField.text = ""
  126.  
  127.  
  128. }
  129.  
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement