1

I've been working for a while on the login part of my app. I'm trying to use ASW Mobile Hub for this matter. I found a way to get it work with the different providers I need: my own user pool, FB and Google.

The problem is that I've been searching here and all over the AWS documentation trying to find the way to get user data (Username and some othe user data like picture, email and so on). I can get it if I'm using the FBSDK directly (usingFBSDKGraphRequest) but I don't know how to do it if the user choose to login in my cognito-user-pool. Also I cannot see what provider the user used once succeeded.

I can find some other ways to get that, but using the old SDK o directly Cognito calls and initially is not what I need. Here's the code I'm using to present the login window:

    override func viewDidLoad() {
       super.viewDidLoad()

       if !AWSSignInManager.sharedInstance().isLoggedIn {
         presentAuthUIViewController()
       }
    }

    func presentAuthUIViewController() {
        let config = AWSAuthUIConfiguration()
        config.enableUserPoolsUI = true
        config.addSignInButtonView(class: AWSFacebookSignInButton.self)
        config.addSignInButtonView(class: AWSGoogleSignInButton.self)


        AWSAuthUIViewController.presentViewController(
        with: self.navigationController!,
        configuration: config, completionHandler: { (provider: 
        AWSSignInProvider, error: Error?) in
            if error == nil {
                // SignIn succeeded.
            } else {
                // end user faced error while loggin in, take any 
                required action here.
            }
        })
     }

So, the question is, how can I get the relevant user info, once the signin is succeeded?

Alex Ra
  • 31
  • 1
  • 7
  • Download the Sample App generated by AWS Mobile Hub and import it to Xcode, after configuring the features. It will contain implementation of some basic features which includes retrieval of user details. – Shravya Feb 01 '18 at 00:57
  • Can you provide me a link of this Sample App, the only thing I can find is several lines of code not conneted to each other, differents methods of doing something that looks silimar but not the same, and some outdated sample apps in Swift 2 that are not useful... thanks – Alex Ra Feb 01 '18 at 09:49
  • You can download the Sample App from your AWS account and sample app will differ based on the features you include. Follow this link: https://docs.aws.amazon.com/pinpoint/latest/developerguide/getting-started-ios-sampleapp.html – Shravya Feb 01 '18 at 17:51
  • Still no way to get the Username. With all the documentation and with this sample, the only thing that I can get (that in fact is very valuable) is the **identityId**. Any other advice? – Alex Ra Feb 02 '18 at 09:04
  • I updated my answer, please check if that helps – Shravya Feb 02 '18 at 18:46
  • @Alex Ra Any luck finding the solution so far ? I am unable to find any thing which will help to find the user basic info. I posted this long ago : https://stackoverflow.com/questions/47548201/unable-to-access-user-details-username-imageurl-after-login-using-aws-sdk-ios/47558820#47558820 – Aseem Feb 04 '20 at 12:34

2 Answers2

0

If the user used cognito login, you can use the below code to get the username.

let identityManager = AWSIdentityManager.default()
let identityUserName = identityManager.identityProfile?.userName

For retrieving the provider once user succeeds, keep it in the session as below

func onLogin(signInProvider: AWSSignInProvider, result: Any?, 
authState: AWSIdentityManagerAuthState, error: Error?) {
  let defaults = UserDefaults.standard
  defaults.set(signInProvider.identityProviderName, forKey: 
     "identityProviderName")
}

Hope this answer helps.

Updated Code to get Username:

let pool = AWSCognitoIdentityUserPool.init(forKey: "CognitoUserPools")
let username = pool.currentUser()?.username
Shravya
  • 208
  • 3
  • 11
0

I've been working on a workaround till I sort this out in a more elegant way. I guess that I need to go deeper in Cognito's understanding. But the fact is even the sample provided by Amazon doesen't show the User's Name...

Sample Amazon app screen

So, in the meantime, I modified the source code of the Cognito library AWSUserPoolsUIOperations to send me the data directly to my app, on a message:

@implementation AWSUserPoolsUIOperations

-(void)loginWithUserName:(NSString *)userName
            password:(NSString *)password
            navigationController:(UINavigationController *)navController
            completionHandler:(nonnull void (^)(id _Nullable, NSError * 
            _Nullable))completionHandler {
            self.userName = userName;

NSDictionary* userInfo = @{@"username": self.userName};

[[NSNotificationCenter defaultCenter]
 postNotificationName:@"UsernameNotification"
 object:nil userInfo:userInfo];

And then just getting the message in the app and storing the value.

    @objc private func TestNotification(_ notification: NSNotification){
    if let dict = notification.userInfo as NSDictionary? {
        if let username = dict["username"] as? String {
            appEstats.username = username
            defaults.set(username, forKey: sUserName)
        }
    }
}

As I said is not the solution but in the meantime it works.

Alex Ra
  • 31
  • 1
  • 7