145 lines
4.6 KiB
Swift
145 lines
4.6 KiB
Swift
//
|
|
// ViewController.swift
|
|
// pocloud
|
|
//
|
|
// Created by Patrick McDonagh on 5/22/18.
|
|
// Copyright © 2018 patrickjmcd. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Alamofire
|
|
import SwiftyJSON
|
|
import SVProgressHUD
|
|
import PromiseKit
|
|
|
|
extension String {
|
|
func fromBase64() -> String? {
|
|
guard let data = Data(base64Encoded: self) else {
|
|
return nil
|
|
}
|
|
return String(data: data, encoding: .utf8)
|
|
}
|
|
|
|
func toBase64() -> String {
|
|
return Data(self.utf8).base64EncodedString()
|
|
}
|
|
}
|
|
|
|
class LoginViewController: UIViewController {
|
|
|
|
let baseURL = (UIApplication.shared.delegate as! AppDelegate).baseURL
|
|
let user = (UIApplication.shared.delegate as! AppDelegate).user
|
|
|
|
let dataFilePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("Auth.plist")
|
|
|
|
|
|
@IBOutlet weak var emailField: UITextField!
|
|
@IBOutlet weak var passwordField: UITextField!
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
loadAuth()
|
|
}
|
|
|
|
@IBAction func signInButtonPressed(_ sender: UIButton) {
|
|
SVProgressHUD.show()
|
|
let email = emailField.text!
|
|
let password = passwordField.text!
|
|
let tokenPart = "\(email):\(password)".toBase64()
|
|
let authToken = "Basic \(tokenPart)"
|
|
|
|
firstly {
|
|
fetchCurrentUser(authToken: authToken)
|
|
}.done { fetchedUser in
|
|
(UIApplication.shared.delegate as! AppDelegate).user = fetchedUser
|
|
self.saveAuth()
|
|
SVProgressHUD.dismiss()
|
|
SVProgressHUD.showSuccess(withStatus: "Login Success")
|
|
self.performSegue(withIdentifier: "logInSegue", sender: self)
|
|
}.catch { _ in
|
|
SVProgressHUD.dismiss()
|
|
SVProgressHUD.showError(withStatus: "Bad Login")
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func fetchCurrentUser(authToken: String) -> Promise<User> {
|
|
let url = "\(baseURL)/users/me"
|
|
|
|
let headers : HTTPHeaders = [
|
|
"Authorization": authToken
|
|
]
|
|
|
|
return Promise<User> { prom in
|
|
Alamofire.request(url, method: .get, headers: headers)
|
|
.validate()
|
|
.responseJSON { response in
|
|
switch response.result {
|
|
case .success(let value):
|
|
let userJSON : JSON = JSON(value)
|
|
let thisUser = User()
|
|
thisUser.userId = userJSON["userId"].intValue
|
|
thisUser.first = userJSON["first"].stringValue
|
|
thisUser.last = userJSON["last"].stringValue
|
|
thisUser.phone = userJSON["phone"].stringValue
|
|
thisUser.addressId = userJSON["addressId"].intValue
|
|
thisUser.companyId = userJSON["companyId"].intValue
|
|
thisUser.email = userJSON["email"].stringValue
|
|
thisUser.authToken = authToken
|
|
prom.fulfill(thisUser)
|
|
|
|
case .failure(let error):
|
|
prom.reject(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
@IBAction func logOutPressed(_ sender: UIBarButtonItem) {
|
|
(UIApplication.shared.delegate as! AppDelegate).user = User()
|
|
saveAuth()
|
|
}
|
|
|
|
func saveAuth(){
|
|
let encoder = PropertyListEncoder()
|
|
|
|
do{
|
|
let data = try encoder.encode(user)
|
|
try data.write(to: dataFilePath!)
|
|
} catch {
|
|
print("Error encoding item array, \(error)")
|
|
}
|
|
}
|
|
|
|
func loadAuth() {
|
|
|
|
if let data = try? Data(contentsOf: dataFilePath!) {
|
|
let decoder = PropertyListDecoder()
|
|
do {
|
|
let storedUser = try decoder.decode(User.self, from: data)
|
|
SVProgressHUD.show()
|
|
|
|
firstly {
|
|
fetchCurrentUser(authToken: storedUser.authToken)
|
|
}.done { currentUser in
|
|
(UIApplication.shared.delegate as! AppDelegate).user = currentUser
|
|
SVProgressHUD.dismiss()
|
|
SVProgressHUD.showSuccess(withStatus: "Found User!")
|
|
self.performSegue(withIdentifier: "logInSegue", sender: nil)
|
|
}.catch { (error) in
|
|
print(error)
|
|
SVProgressHUD.dismiss()
|
|
}
|
|
} catch {
|
|
print("Error decoding: \(error)")
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|