0

I am starting to find my way around using AWS. But I have a simple question for which I have not found an answer by searching the net an trying what came to my mind. Here is the relevant working code:

override func viewDidLoad() {
    super.viewDidLoad()
    ...

    if AWSSignInManager.sharedInstance().isLoggedIn {
        print("We are already logged in!")
        // I would like to display the user identification of the current user.
    }

    ...
}

At the place of the comment I would like to print the user ID of the current user (when someone is logged in). I presume there has to be a way. How can I do that?

Michel
  • 10,303
  • 17
  • 82
  • 179
  • This may help https://stackoverflow.com/questions/50988499/aws-cognito-sign-in-not-working-swift-ios – Dharmesh Kheni May 15 '19 at 10:03
  • Yes indeed that's already an improvement. I am not totally sure this is what I should consider as a user ID though. Using the code in the post I can get: AccessKey, SecretKey and a SessionKey. Because that seems to change every time I launch the app. – Michel May 15 '19 at 10:39
  • Looking a bit more into the link mentioned by Dharmesh, seems to indicate that I should be able to get more than AccessKey, SecretKey and a SessionKey (contrary to what I fisrt thought). But I would like to find something simpler (if possible). – Michel May 15 '19 at 16:25

2 Answers2

1

Maybe consider the Cognito "username" field, which is the unique ID (immutable) for each user:

        let amc = AWSMobileClient.sharedInstance();
        if (amc.isSignedIn) {
             print ("Signed in with username=\(amc.username!)")
        }

we can also use getUserAttributes to fetch all the attributes like so:

            // See all the attributes the user has
            amc.getUserAttributes { (attributes, error) in
                if let attributes = attributes {
                    for (key, value) in attributes {
                        print("\(key): \(value)")
                    }
                } else if let error = error {
                    print("Unexpected error: \(error.localizedDescription)")
                }
            }
peekay
  • 1,885
  • 13
  • 11
  • Thanks, the first part of your suggestion does not seem very promising as far as I can see. Following what happens under the debugger, I see this: (lldb) p amc.isSignedIn (Bool) $R10 = false (lldb) p amc.username (String?) $R12 = "myuserguy" The username is correct but surprisingly isSignedIn appears as false. – Michel May 16 '19 at 02:01
  • Fortunately the second part sounds better. I get these attributes: ATTRIBUTE :: phone_number_verified: false ATTRIBUTE :: email_verified: true ATTRIBUTE :: email: cwn52510@cndps.com ATTRIBUTE :: sub: a2bz6zc3-xyzt-1234-809f-51214f736a80 ATTRIBUTE :: phone_number: +839022331234 It seems like the 4th attribute called "sub" could be what I need. – Michel May 16 '19 at 02:05
  • I can indeed retrieve this number for the given user in the AWS console. – Michel May 16 '19 at 02:21
  • oh, the first part might be a bug in the aws sdk: https://github.com/aws-amplify/aws-sdk-ios/issues/1314 – peekay May 16 '19 at 09:01
0

I believe the userId you are looking for is AWSMobileClient.default().getIdentityId()

https://aws-amplify.github.io/aws-sdk-ios/docs/reference/AWSMobileClient/Classes/AWSMobileClient.html#/c:@CM@AWSMobileClient@objc(cs)AWSMobileClient(im)getIdentityId

Lawmicha
  • 147
  • 6