Search code examples
amazon-web-servicesswift3amazon-cognitofederated-identityaws-mobilehub

AWS MobileHub - Getting user profile details after logging in via GOOGLE and FB


I am using AWS Mobile Hub for iOS and wanted help getting user details by using AWS Cognito identity pools.

For example after logging-in via Google and Facebook, along with the secret keys which Google and FB give, I also need the user-profile details like his Email-ID, Profile_picture from them.

It will be good if someone can post a SWIFT code for the same.


Solution

  • The following code snippets use the Facebook and Google SDK for iOS respectively.

    For Facebook:

            import FBSDKCoreKit
            import FBSDKLoginKit
    
            let imageGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "picture.type(large)"])
            let imageConnection = FBSDKGraphRequestConnection()
            imageConnection.add(imageGraphRequest, completionHandler: { (connection, result, error) in
                guard let imageResult = result as? NSDictionary else  { return}
                if let imageURL = URL(string:(((imageResult.value(forKey: "picture") as AnyObject).value(forKey: "data") as AnyObject).value(forKey: "url") as? String)!) {
                    self.imageURL = imageURL
                }
            })
            imageConnection.start()
    
            let userGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, email"])
            let userConnection = FBSDKGraphRequestConnection()
            userConnection.add(userGraphRequest, completionHandler: { (connection, result, error) in
                guard let userResult = result as? NSDictionary else { return }
                    if let userName = userResult.value(forKey: "name")  as? String {
                        self.userName = userName
                    }
            })
            userConnection.start()
    

    For Google:

    import GoogleSignIn
    
    
    let googleUser = GIDSignIn.sharedInstance().currentUser
    self.userName = googleUser?.profile.name
    self.imageURL = googleUser?.profile.imageURL(withDimension: GoogleSignInProviderProfileImageDimension)