Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get to AccessToken and IdToken following successful Amazon Cognito Login from iOS

I have been using the following sample to introduce cognito login to my iOS application:

https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoYourUserPools-Sample

This is working well and I am at the point where I have a AWSCognitoIdentityUserPool object which I can call the currentUser() method to access the user.

What I am struggling to find is how I extract both the AccessToken and the IdToken.

In the android equivalent, the onSuccess method of the AuthenticationHandler has a CognitoUserSession argument which in turn has getIdToken() and getAccessToken().

Frustratingly, I see them output as the AuthenticationResult in json format in the output window, but I just don't know how to access them programatically?

like image 468
Gary Wright Avatar asked Mar 10 '23 20:03

Gary Wright


1 Answers

Figured it out now:

func getSession(){
    self.user?.getSession().continueOnSuccessWith { (getSessionTask) -> AnyObject? in
        DispatchQueue.main.async(execute: {
            let getSessionResult = getSessionTask.result
            self.idToken = getSessionResult?.idToken?.tokenString
            self.accessToken = getSessionResult?.accessToken?.tokenString

        })
        return nil
    }
}
like image 122
Gary Wright Avatar answered Mar 13 '23 08:03

Gary Wright