Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.62 KB | None | 0 0
  1. import Alamofire
  2. import SwiftyJSON
  3.  
  4. ///HTTP-Layer
  5. ///Creates with DeviceToken the URL to request the user-credentials
  6. class TokenConverterConnector {
  7.    
  8.     static let GOOGLETOKENURL = "https://www.googleapis.com/oauth2/v1/userinfo?access_token="
  9.    
  10.     static func requestGoogleCredentials(accessToken:String, completion: @escaping (_ jsonObject: SwiftyJSON?) -> Void) {
  11.    
  12.         let url = GOOGLETOKENURL + accessToken
  13.        
  14.         Alamofire.request(url).responseJSON {
  15.             response in
  16.            
  17.             var jsonValue:SwiftyJSON?
  18.            
  19.             if let JSON = response.result.value {
  20.                 jsonValue = SwiftyJSON(JSON)
  21.             }
  22.            
  23.             completion(jsonValue)
  24.         }
  25.     }
  26. }
  27.  
  28. ///Service-Layer
  29. ///Converts Device Token of the User into his E-Mail, which is necessary for the Backend.
  30. class TokenConverterService {
  31.    
  32.     static func extractGoogleToken(accessToken:String, completion: @escaping (_ extraionSuccessful:Bool) -> Void) {
  33.        
  34.         TokenConverterConnector.requestGoogleCredentials(accessToken: accessToken, completion: {
  35.             (jsonObject) in
  36.            
  37.             var extractionSuccessful:Bool = false
  38.            
  39.             if let json = jsonObject {
  40.                
  41.                 let email = json["email"].string
  42.                
  43.                 if let newMail = email {
  44.                    
  45.                     SettingsService.saveMail(newMail)
  46.                     extractionSuccessful = true
  47.                 }
  48.             }
  49.            
  50.             completion(extractionSuccessful)
  51.         })
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement