SHARE
TWEET

Untitled

a guest May 26th, 2018 140 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  FirebaseHelper.swift
  3. //  shopper
  4. //
  5. //  Created by Daniel Cabrera on 3/6/18.
  6. //  Copyright © 2018 Cuevas Cabrera. All rights reserved.
  7. //
  8. import Foundation
  9. import Firebase
  10. import FirebaseDatabase
  11. import FirebaseAuth
  12.  
  13. struct FirebaseHelper {
  14.    
  15.     enum FirebaseDBKeys : String {
  16.         case users
  17.         case articles
  18.     }
  19.    
  20.     enum FirebaseError {
  21.         case UserNotFound
  22.         case Unknown
  23.     }
  24.    
  25.     static var ref: DatabaseReference! = {
  26.         return Database.database().reference()
  27.     }()
  28.    
  29.    
  30.     static func SignInFirebase(aEmail: String, aPass: String, completionHandler: @escaping (Bool, String) -> ()) {
  31.         Auth.auth().signIn(withEmail: aEmail, password: aPass) { (user, error) in
  32.             guard error == nil else {
  33.                 completionHandler(false,"Email/username in use try again")
  34.                 return
  35.             }
  36.             completionHandler(true, "User sign in")
  37.         }
  38.     }
  39.    
  40.     static func isAlreadySignIn(completionHandler: @escaping (Bool, String) -> ()) {
  41.         guard Auth.auth().currentUser != nil else {
  42.              completionHandler(false, "User not log in")
  43.             return
  44.         }
  45.             completionHandler(true, "User log in")
  46.     }
  47.    
  48.     static func SignOut(completionHandler: @escaping (Bool, String) -> ()) {
  49.         do {
  50.             try Auth.auth().signOut()
  51.             completionHandler(true, "Sign Out")
  52.         } catch  {
  53.             completionHandler(false, "Error Sign Out")
  54.         }
  55.     }
  56.    
  57.     static func updatePassword(currentPassword: String, newPassword: String, completionHandler: @escaping (FirebaseError?) -> ()) -> () {
  58.         guard let user = Auth.auth().currentUser else {
  59.             completionHandler(.UserNotFound)
  60.             return
  61.         }
  62.        
  63.         SignInFirebase(aEmail: user.email!, aPass: currentPassword) { response, _ in
  64.             if response {
  65.                 Auth.auth().currentUser?.updatePassword(to: newPassword, completion: { error in
  66.                     if error == nil {
  67.                         SignOut(completionHandler: { _,_ in })
  68.                         completionHandler(nil)
  69.                     } else {
  70.                         completionHandler(.Unknown)
  71.                     }
  72.                 })
  73.             } else {
  74.                 completionHandler(.UserNotFound)
  75.             }
  76.         }
  77.     }
  78.    
  79.     static func CreateFirebaseUser(aEmail: String, aPass: String, completionHandler: @escaping (Bool, String) -> ()) {
  80.         Auth.auth().createUser(withEmail: aEmail, password: aPass) { (user, error) in
  81.             guard error == nil else {
  82.                 completionHandler(false,"Error handling user")
  83.                 return
  84.             }
  85.             debugPrint(user?.email ?? "Not email")
  86.             debugPrint("Success creating user")
  87.             completionHandler(true,"User created")
  88.         }
  89.     }
  90.    
  91.     static func uploadImage(image: UIImage, completion: @escaping (URL?,String) -> ()) {
  92.         guard let data = UIImagePNGRepresentation(image) else { return completion(nil, "Error formatting image ")}
  93.         let storageRef = Storage.storage().reference()
  94.         let riversRef =  storageRef.child("\(UUID().uuidString).png")
  95.         _ = riversRef.putData(data, metadata: nil) { (metadata, error) in
  96.             guard let metadata = metadata else {
  97.                 completion(nil,"Error saving image")
  98.                 return
  99.             }
  100.             // Metadata contains file metadata such as size, content-type, and download URL.
  101.             completion(metadata.downloadURL(), "Success storing image")
  102.         }
  103.     }
  104.    
  105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top