// // SettingsViewController.swift // Vendoo // // Created by Okechi Onyeje on 6/12/16. // Copyright © 2016 Okechi Onyeje. All rights reserved. // import UIKit import Locksmith import FirebaseAuth import FirebaseDatabase import FirebaseStorage /// This class is responsible for updating current users password and deleting current user account class SettingsViewController: UIViewController { //IBOutlet @IBOutlet weak var closeButton: UIBarButtonItem! @IBOutlet weak var table: UITableView! var firbase = FirebaseManager() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.table.dataSource = self self.table.delegate = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } //MARK: - IBActions extension SettingsViewController { @IBAction func dismissSettings(sender: AnyObject) { let vc = self.storyboard?.instantiateViewControllerWithIdentifier("Reveal View Controller") self.presentViewController(vc!, animated: false, completion: nil) } } extension SettingsViewController: UITableViewDataSource { func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return 2 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: UITableViewCell switch(indexPath.row){ case 0: cell = self.table.dequeueReusableCellWithIdentifier("Change_Pass_Cell")! break default: cell = self.table.dequeueReusableCellWithIdentifier("Delete_Account_Cell")! break } return cell } } extension SettingsViewController: UITableViewDelegate { func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { switch(indexPath.row){ case 0: let alert = UIAlertController(title: "Change Password", message: "WARNING! You are about to change your vendoo account password. Please enter your current password to continue.", preferredStyle: .Alert) alert.addTextFieldWithConfigurationHandler({ (textField : UITextField!) -> Void in textField.placeholder = "Enter Current Password" }) alert.addAction(UIAlertAction(title: "Continue", style: .Default, handler:{(action: UIAlertAction!) in let textField1 = alert.textFields![0] as UITextField let dictionary = Locksmith.loadDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo") if(textField1.text == dictionary!["pass"] as? String){ print("password accepted please enter new password") let alert2 = UIAlertController(title: "Change Password", message: "WARNING! You are about to change your vendoo account password. Please enter your new password to continue.", preferredStyle: .Alert) alert2.addTextFieldWithConfigurationHandler({ (textField : UITextField!) -> Void in textField.placeholder = "Enter New Password" }) alert2.addAction(UIAlertAction(title: "Change Password", style: .Default, handler:{(action: UIAlertAction!) in let textField2 = alert2.textFields![0] as UITextField FIRAuth.auth()?.currentUser?.updatePassword(textField2.text!, completion: { (error) -> Void in if (error != nil) { print(error) }else { print("Current user's new password is now \(textField2.text!).") do{ try Locksmith.updateData(["pass": textField2.text!], forUserAccount: (NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo") print("account credentials saved") } catch{ //could not save data to keychain print("account credentials could not be saved") } } }) })) alert.dismissViewControllerAnimated(false, completion: {}) self.presentViewController(alert2, animated: true, completion: nil) }else{ print("incorrect password") let alert2 = UIAlertController(title: "Incorrect Password", message: "Incorrect password. Please try again.", preferredStyle: .Alert) alert2.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in alert2.dismissViewControllerAnimated(false, completion: {}) alert.textFields![0].text = "" self.presentViewController(alert, animated: true, completion: nil) })) alert.dismissViewControllerAnimated(false, completion: { Void in}) self.presentViewController(alert2, animated: true, completion: nil) } })) alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action: UIAlertAction!) in})) self.presentViewController(alert, animated: true, completion: nil) break default: let alert = UIAlertController(title: "Delete Account", message: "WARNING! You are about to delete your vendoo account. This action will not only delete your account information, but delete any listing history and network authorizations you may currently have. To proceed please enter your account password.", preferredStyle: .Alert) alert.addTextFieldWithConfigurationHandler({ (textField : UITextField!) -> Void in textField.placeholder = "Enter Password" }) alert.addAction(UIAlertAction(title: "Delete", style: .Default, handler:{(action: UIAlertAction!) in let textField = alert.textFields![0] as UITextField let dictionary = Locksmith.loadDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo") if(textField.text == dictionary!["pass"] as? String){ print("account deleted, add in deletion code as well as deauthorization code for any network currently authorized") //get all listing information for current user and delete from gloabl listings in firebase print((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!) self.firbase.ref.child("Users/\(self.firbase.user_email)").observeSingleEventOfType( .Value, withBlock: { (snapshot) -> Void in let userDict = snapshot.value as? [String : AnyObject] let listingDict = userDict!["user_Listings"] as? [String : AnyObject] if listingDict != nil { for (key, values) in listingDict! { self.firbase.ref.child("Global_listings").child(key).removeValue() FIRStorage.storage().referenceForURL((userDict!["image_storage"] as? String)!).child("images/\(key)").deleteWithCompletion({ (error) -> Void in }) } } //delete all user accounts currently assouciated with user self.firbase.ref.child("Users").child(self.firbase.user_email).removeValueWithCompletionBlock({ (error, ref) -> Void in if error == nil { FIRAuth.auth()?.currentUser?.deleteWithCompletion({ (profile) -> Void in do{ try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo") do { try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo_oauth_ebay") }catch { (error) print(error) } do { try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo_oauth_etsy") }catch { (error) print(error) } do { try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo_oauth_amazon") }catch { (error) print(error) } for key in Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys) { NSUserDefaults.standardUserDefaults().removeObjectForKey(key) } let vc = self.storyboard?.instantiateViewControllerWithIdentifier("SignInViewController") self.presentViewController(vc!, animated: true, completion: nil) } catch{ (error) print(error) } }) } }) }) alert.dismissViewControllerAnimated(true, completion: nil) }else{ print("incorrect password") let alert2 = UIAlertController(title: "Incorrect Password", message: "Incorrect password. Please try again.", preferredStyle: .Alert) alert2.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in alert2.dismissViewControllerAnimated(false, completion: {}) alert.textFields![0].text = "" self.presentViewController(alert, animated: true, completion: nil) })) alert.dismissViewControllerAnimated(false, completion: { Void in}) self.presentViewController(alert2, animated: true, completion: nil) } })) alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action: UIAlertAction!) in})) self.presentViewController(alert, animated: true, completion: nil) break } } }