mirror of
https://bitbucket.org/vendoo/vendoo_v1.0.git
synced 2025-12-25 11:47:40 +00:00
213 lines
8.7 KiB
Swift
213 lines
8.7 KiB
Swift
//
|
|
// FirstViewController.swift
|
|
// Vendoo
|
|
//
|
|
// Created by Okechi Onyeje on 5/22/16.
|
|
// Copyright © 2016 Okechi Onyeje. All rights reserved.
|
|
//
|
|
|
|
|
|
import UIKit
|
|
import FirebaseAuth
|
|
import Locksmith
|
|
|
|
|
|
class SignInViewController: UIViewController {
|
|
//class variables
|
|
var isValidated: Bool! = false
|
|
|
|
//IBOutlets
|
|
@IBOutlet weak var email: UITextField!
|
|
@IBOutlet weak var password: UITextField!
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
// Do any additional setup after loading the view, typically from a nib.
|
|
|
|
|
|
//set up delegates
|
|
self.email.delegate = self
|
|
self.password.delegate = self
|
|
|
|
//check if there is a user logged in
|
|
if NSUserDefaults.standardUserDefaults().boolForKey("signedIn"){
|
|
|
|
//load user account from keychain
|
|
self.email.text = NSUserDefaults.standardUserDefaults().objectForKey("email") as? String
|
|
|
|
let dictionary = Locksmith.loadDataForUserAccount(self.email.text!, inService: "vendoo")
|
|
self.password.text = dictionary!["pass"] as? String
|
|
|
|
signInUser(self)
|
|
}else{
|
|
print("user not found")
|
|
}
|
|
|
|
}
|
|
|
|
override func didReceiveMemoryWarning() {
|
|
super.didReceiveMemoryWarning()
|
|
// Dispose of any resources that can be recreated.
|
|
}
|
|
|
|
}
|
|
|
|
//IBActions
|
|
extension SignInViewController {
|
|
//sign in current or returning user
|
|
@IBAction func signInUser(sender: AnyObject) {
|
|
//add validation
|
|
let finalEmail = email!.text!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
|
|
|
|
if isValidEmail(finalEmail){
|
|
FIRAuth.auth()?.signInWithEmail(email.text!, password: password.text!) { (user, error) in
|
|
if error != nil {
|
|
//alert for wrong pass
|
|
if ( error!.userInfo["error_name"] as! String == "ERROR_WRONG_PASSWORD"){
|
|
|
|
var alert = UIAlertController(title: "Incorrect Password", message: "The password was invalid for the email given, please reenter the correct password.", preferredStyle: .Alert)
|
|
|
|
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {(action: UIAlertAction!) in
|
|
}))
|
|
self.presentViewController(alert, animated: true, completion: nil)
|
|
|
|
//alert for invalid email
|
|
}else if (error!.userInfo["error_name"] as! String == "ERROR_INVALID_EMAIL"){
|
|
|
|
var alert = UIAlertController(title: "Email Invalid", message: "The email given was not valid, please enter a valid email.", preferredStyle: .Alert)
|
|
|
|
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {(action: UIAlertAction!) in
|
|
}))
|
|
self.presentViewController(alert, animated: true, completion: nil)
|
|
|
|
//alert for disabled account
|
|
}else if (error!.userInfo["error_name"] as! String == "ERROR_USER_DISABLED"){
|
|
var alert = UIAlertController(title: "User Account Disabled", message: "Your account has temporarily been disabled, please contact support for more information.", preferredStyle: .Alert)
|
|
|
|
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {(action: UIAlertAction!) in
|
|
}))
|
|
self.presentViewController(alert, animated: true, completion: nil)
|
|
|
|
//if request fails due to connectivity, then retry the login request
|
|
}else if (error?.userInfo["NSUnderlyingError"]?.code == -1005 || error?.code == 17020){
|
|
|
|
var alert = UIAlertController(title: "Network Error", message: "There has been a problem connecting to the server. This could be due to various reasons such as having a slow or non existent connection or using an anonymity tool like a VPN service. Would you like to attempt to reconnect?", preferredStyle: .Alert)
|
|
|
|
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {(action: UIAlertAction!) in
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {() -> Void in
|
|
var downloadGroup = dispatch_group_create()
|
|
dispatch_group_enter(downloadGroup)
|
|
dispatch_group_wait(downloadGroup, dispatch_time(DISPATCH_TIME_NOW, 5000000000))
|
|
// Wait 5 seconds before trying again.
|
|
dispatch_group_leave(downloadGroup)
|
|
dispatch_async(dispatch_get_main_queue(), {() -> Void in
|
|
//Main Queue stuff here
|
|
//Redo the function that made the Request.
|
|
self.signInUser(self)
|
|
|
|
})
|
|
})
|
|
}))
|
|
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action: UIAlertAction!) in
|
|
}))
|
|
self.presentViewController(alert, animated: true, completion: nil)
|
|
|
|
return
|
|
}
|
|
} else {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), { () -> Void in
|
|
|
|
/*If the user has not already signed in then save their log in info*/
|
|
if !(NSUserDefaults.standardUserDefaults().boolForKey("signedIn")){
|
|
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "signedIn")
|
|
NSUserDefaults.standardUserDefaults().setObject(self.email.text, forKey: "email")
|
|
|
|
|
|
//save data to keychain
|
|
do{
|
|
try Locksmith.saveData(["pass": self.password.text!], forUserAccount: self.email.text!, inService: "vendoo")
|
|
print("account credentials saved")
|
|
}
|
|
catch{
|
|
//could not save data to keychain
|
|
print("account credentials could not be saved")
|
|
|
|
}
|
|
print("User saved")
|
|
}
|
|
|
|
self.performSegueWithIdentifier("HomeScreenSegue", sender:nil)
|
|
|
|
})
|
|
}
|
|
}
|
|
}else {
|
|
//alert for invalid email
|
|
let alert = UIAlertController(title: "Bad Email Given", message: "The email given was not valid. Please enter a valid email", preferredStyle: .Alert)
|
|
|
|
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {(action: UIAlertAction!) in
|
|
}))
|
|
self.presentViewController(alert, animated: true, completion: nil)
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Mark: - TextField Delegate Methods
|
|
extension SignInViewController: UITextFieldDelegate {
|
|
|
|
func textFieldShouldReturn(textField: UITextField) -> Bool {
|
|
//dismissKeyboard()
|
|
self.view.endEditing(true)
|
|
return false
|
|
}
|
|
|
|
}
|
|
|
|
//MARK: - Navigation Methods
|
|
extension SignInViewController {
|
|
|
|
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
|
|
switch (segue.identifier!) {
|
|
|
|
case "HomeScreenSegue":
|
|
//logic to prepare user for application use after firebase login
|
|
//Testing OAuth
|
|
|
|
break
|
|
default:
|
|
//default logic for any other segues
|
|
break
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//Private Class Helper Methods
|
|
extension SignInViewController {
|
|
//Validate Email/Username
|
|
func isValidEmail(testStr:String) -> Bool {
|
|
|
|
print("Validate emilId: \(testStr)")
|
|
|
|
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
|
|
|
|
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
|
|
|
|
let result = emailTest.evaluateWithObject(testStr)
|
|
|
|
return result
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|