Advertisement
Guest User

Firebase Connection

a guest
Dec 1st, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.55 KB | None | 0 0
  1. //
  2. //  FirebaseConnection.swift
  3. //  VoxWebMusic
  4. //
  5. //  Created by Danilo Mozer on 27/11/17.
  6. //  Copyright © 2017 Diet Code Apps. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import Firebase
  11. import FirebaseAuth
  12. import FirebaseStorage
  13. import FirebaseDatabase
  14.  
  15. struct FirebaseConnection{
  16.    
  17.     var databaseRef : DatabaseReference{
  18.         return Database.database().reference()
  19.     }
  20.    
  21.     var storageRef : StorageReference{
  22.         return Storage.storage().reference()
  23.     }
  24.    
  25.     // 1 --- We Create the User
  26.     func signUp(email: String, firstName: String, lastName: String, password: String, data: NSData!){
  27.        
  28.         Auth.auth().createUser(withEmail: email, password: password, completion:{ (user, error) in
  29.            
  30.             if error == nil {
  31.                
  32.                 self.setUserInfo(user: user!, firstName: firstName, lastName: lastName, password: password, data: data)
  33.                
  34.             }else{
  35.                 print(error!.localizedDescription)
  36.                
  37.             }
  38.            
  39.         })
  40.        
  41.     }
  42.    
  43.     // 2 --- Set user Info
  44.    
  45.     private func setUserInfo(user: User, firstName: String, lastName: String, password: String, data: NSData){
  46.        
  47.         //Create Path for the user image
  48.        
  49.         let imagePath = "profile\(user.uid)/userPic.jpg"
  50.        
  51.         //Create image reference
  52.        
  53.         let imageRef = storageRef.child(imagePath)
  54.        
  55.         //Create metadata for the image
  56.        
  57.         let metaData = StorageMetadata()
  58.        
  59.         //Save the user image in the Firebase Storage File
  60.        
  61.         imageRef.putData(data as Data, metadata: metaData) { (data, error) in
  62.             if error == nil{
  63.                
  64.                 let changeRequest = user.createProfileChangeRequest()
  65.                 changeRequest.displayName = ("\(firstName) \(lastName)")
  66.                 changeRequest.photoURL = metaData.downloadURL()
  67.                 changeRequest.commitChanges(completion: { (error) in
  68.                    
  69.                     if error == nil {
  70.                         self.saveInfo(user: user, firstName: firstName, lastName: lastName, password: password)
  71.                     }
  72.                    
  73.                 })
  74.                
  75.             }else{
  76.                 print(error!.localizedDescription)
  77.             }
  78.            
  79.         }
  80.        
  81.        
  82.        
  83.     }
  84.    
  85.    
  86.     // 3 --- Saving user info
  87.     private func saveInfo(user: User, firstName: String, lastName: String, password: String){
  88.        
  89.         // Create user dictionary info
  90.        
  91.         let userInfo = ["email": user.email!,"firstName": firstName,"lastName": lastName,"photoURL": (user.photoURL!)] as [String : Any]
  92.        
  93.         //Create reference
  94.        
  95.         let userRef = databaseRef.child("users").child(user.uid)
  96.        
  97.         //Save the user info in the Database
  98.        
  99.         userRef.setValue(userInfo)
  100.        
  101.         signIn(email: user.email!, password: password)
  102.        
  103.     }
  104.    
  105.     //4 --- Signing In
  106.    
  107.     func signIn(email: String, password: String){
  108.        
  109.         Auth.auth().signIn(withEmail: email, password: password, completion: { (user,error) in
  110.            
  111.             if error == nil{
  112.                 if let user = user {
  113.                     print ("\(user.displayName!) has signed in sucessfully!")
  114.                 }
  115.             }else{
  116.                 print(error!.localizedDescription)
  117.                
  118.             }
  119.         })
  120.  
  121.     }
  122.    
  123.  
  124.  
  125.    
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement