Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MainViewController.swift
- // Witit-iOS
- //
- // Created by Nikolas Andryuschenko on 8/2/16.
- // Copyright © 2016 Witit Co. All rights reserved.
- //
- import UIKit
- import Firebase
- class MainViewController: UIViewController {
- @IBOutlet weak var usernameLabel: UILabel!
- @IBOutlet weak var emailTextField: UITextField!
- @IBOutlet weak var passwordTextField: UITextField!
- @IBOutlet weak var logoutButton: UIButton!
- override func viewDidLoad() {
- super.viewDidLoad()
- if let user = FIRAuth.auth()?.currentUser {
- self.logoutButton.alpha = 1.0
- self.usernameLabel.text = user.email
- } else {
- self.logoutButton.alpha = 0.0
- self.usernameLabel.text = ""
- }
- // Do any additional setup after loading the view.
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- @IBAction func createAccountButtonTapped(sender: UIButton) {
- if self.emailTextField.text == "" || self.passwordTextField.text == "" {
- // put alert controllers in error handeling swift file
- let alertController = UIAlertController(title: "Error", message: "Please enter email and password.", preferredStyle: .Alert)
- let defaultAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
- alertController.addAction(defaultAction)
- self.presentViewController(alertController, animated: true, completion: nil)
- } else {
- FIRAuth.auth()?.createUserWithEmail(self.emailTextField.text!, password: self.passwordTextField.text!, completion: { (user, error) in
- if error == nil {
- self.logoutButton.alpha = 0
- self.usernameLabel.text = user!.email
- self.emailTextField.text = ""
- self.passwordTextField.text = ""
- } else {
- //Change location of alertcontroller
- let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .Alert)
- let defaultAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
- alertController.addAction(defaultAction)
- }
- })
- }
- }
- @IBAction func loginButtonTapped(sender: UIButton) {
- if self.emailTextField.text == "" || self.passwordTextField.text == "" {
- // put alert controllers in error handeling swift file
- let alertController = UIAlertController(title: "Error", message: "Please enter email and password.", preferredStyle: .Alert)
- let defaultAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
- alertController.addAction(defaultAction)
- self.presentViewController(alertController, animated: true, completion: nil)
- } else {
- FIRAuth.auth()?.signInWithEmail(self.emailTextField.text!, password: self.emailTextField.text!, completion: { (user, error) in
- if error == nil {
- self.logoutButton.alpha = 0
- self.usernameLabel.text = user!.email
- self.emailTextField.text = ""
- self.passwordTextField.text = ""
- } else {
- //Change location of alertcontroller
- let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .Alert)
- let defaultAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
- alertController.addAction(defaultAction)
- }
- })
- }
- }
- @IBAction func logoutActionButtonTapped(sender: UIButton) {
- try! FIRAuth.auth()?.signOut()
- self.usernameLabel.text = ""
- self.logoutButton.alpha = 0
- self.emailTextField.text = ""
- self.passwordTextField.text = ""
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement