Advertisement
Yacine123

Untitled

Aug 28th, 2016
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.59 KB | None | 0 0
  1.     func handleLoginFb(){
  2.    
  3.        
  4.        
  5.         let facebookLogin = FBSDKLoginManager()
  6.        
  7.         print("LoggedIn")
  8.        
  9.         facebookLogin.logInWithReadPermissions(["public_profile","email","user_friends"], fromViewController: self, handler: {(facebookResults,facebookError) -> Void in
  10.        
  11.             if facebookError != nil {
  12.            
  13.                 print("facebook error login")
  14.                 return
  15.             }else if facebookResults.isCancelled{
  16.                
  17.                 print("facebook login is canceled")
  18.            
  19.             }else {
  20.            
  21.             print("you are i in")
  22.  
  23.                 let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
  24.                 FIRAuth.auth()?.signInWithCredential(credential){ (user, error) in
  25.                    
  26.                     print("user loged in firebase")
  27.                 }
  28.                
  29.                 self.dismissViewControllerAnimated(true, completion: nil)
  30.                    
  31.                 }
  32.  
  33.            
  34.        
  35.        
  36.         } );
  37.    
  38.    
  39.     }
  40.    
  41.    
  42.    
  43.     func anonymousLogin(){
  44.    
  45.         FIRAuth.auth()?.signInAnonymouslyWithCompletion() { (user, error) in
  46.            
  47.             if (error != nil){
  48.                 print(error)
  49.                 return
  50.            
  51.             }
  52.  
  53.             print("user Loged anonymously with " + user!.uid )
  54.             self.dismissViewControllerAnimated(true, completion: nil)
  55.  
  56.            
  57.         }
  58.    
  59.    
  60.    
  61.     }
  62.  
  63.  
  64.  
  65.     func handleLogin(){
  66.    
  67.         guard let email = emailTextField.text, password = passwordTextField.text
  68.             else {
  69.                 print("Form is not valid")
  70.                 return
  71.         }
  72.         FIRAuth.auth()?.signInWithEmail(email, password: password, completion: {(user, error ) in
  73.        
  74.             if (error != nil){
  75.                 print(error)
  76.                 return
  77.             }
  78.            
  79.    
  80.             self.dismissViewControllerAnimated(true, completion: nil)
  81.            
  82.            
  83.         })
  84.        
  85.     }
  86.  
  87.  
  88.  
  89.    
  90.     func handleRegister(){
  91.        
  92.         guard let email = emailTextField.text, name = nameTextField.text , password = passwordTextField.text
  93.             else {
  94.         print("Form is not valid")
  95.         return
  96.         }
  97.        
  98.         FIRAuth.auth()?.createUserWithEmail(email, password:password, completion:{(user: FIRUser?, error) in
  99.            
  100.             if(error != nil){
  101.                 print(error)
  102.                 return
  103.            
  104.             }
  105.            
  106.             //User successfuly authentified
  107.             print("User successfuly authentified ")
  108.            
  109.             guard let uid = user?.uid else {
  110.                 return
  111.             }
  112.            
  113.             let ref = FIRDatabase.database().referenceFromURL("https://e-commerce-cpa-57e7d.firebaseio.com/")
  114.             let usersReference = ref.child("users").child(uid)
  115.            
  116.             let values = ["name": name , "email": email ]
  117.            
  118.             usersReference.updateChildValues(values,withCompletionBlock: {(err, ref) in
  119.                
  120.                 if (err != nil){
  121.                     print("error occured")
  122.  
  123.                 print(err)
  124.                 return
  125.                 }
  126.             print("User succesfuly registred")
  127.            
  128.                
  129.             })
  130.            
  131.        
  132.        
  133.        
  134.         })
  135.         self.dismissViewControllerAnimated(true, completion: nil)
  136.  
  137.  
  138.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement