Skip to content

Instantly share code, notes, and snippets.

@xcadaverx
Created June 17, 2016 17:26
Show Gist options
  • Save xcadaverx/e00ca2c5606fc0c7aa3feeb924f786e6 to your computer and use it in GitHub Desktop.
Save xcadaverx/e00ca2c5606fc0c7aa3feeb924f786e6 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// YogiDoListFB
//
// Created by Daniel Williams on 6/16/16.
// Copyright © 2016 Dandom, LLC. All rights reserved.
//
import UIKit
import FirebaseAuth
enum SignInError: ErrorType {
case UserNotFound
case InvalidPassword
case OtherError(code: Int)
}
class SignInViewController: UIViewController {
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var loginButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func loginButtonPressed(sender: UIButton) {
guard let username = usernameTextField.text where !username.isEmpty else { return }
guard let password = passwordTextField.text where !password.isEmpty else { return }
signUserIn(username, password: password)
}
func signUserIn(username: String, password: String) {
FIRAuth.auth()?.signInWithEmail(username, password: password, completion: { (user, error) in
guard error == nil else {
self.handleError(error!) // how to handle this error case without passing an individual SignInError.XXXX
return
}
user?.getTokenWithCompletion({ (token, error) in
guard error == nil else { self.handleError(SignInError.OtherError(code: error!.code)); return }
print(token)
})
})
}
func handleError(error: ErrorType) {
switch error {
case SignInError.InvalidPassword:
print("invalid password.")
case SignInError.UserNotFound:
print("user not found!")
case SignInError.OtherError(let code):
print("some other error: \(code)")
default:
print("how'd we get here?")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment