Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift, AWS Cognito User Pool, Unable to Refresh Access Token

Looked at similar SO questions which didn't help me resolve the issue.

I am using AWS Cognito User Pools in our iOS App. We are able to successfully create and login the user. However after about an Hr the access token is not available, I understand from AWS Cognito documentation that the iOS SDK automatically refreshes (also mentioned here) and obtains the token when it is not available, however I don't see this behaviour. The below code shows how I am trying to obtain the access token.

Using iOS SDK AWSCognitoIdentityProvider 2.6.7

Please advice how I can resolve the issue.

let mySession = self.pool.currentUser()?.getSession()

guard let accessToken = mySession?.result?.accessToken?.tokenString as? String else {
  print("Unable to obtain access token")
  self.handleSignOut() // Signing out the user since there is no access token       
  return
}
like image 653
SpaceX Avatar asked Oct 17 '22 21:10

SpaceX


1 Answers

getSession() returns an AWSTask.

You have to access the tokenString in the callback.

The following code works for me:

self.pool.currentUser()?.getSession().continueWith { task in

    if task.error == nil {
        let session = task.result! as AWSCognitoIdentityUserSession
        guard let accessToken = session.idToken?.tokenString; as? String else {
           print("Unable to obtain access token")
           self.handleSignOut() // Signing out the user since there is no access token       
        }
    }
}
like image 148
Kevin Sun Avatar answered Oct 20 '22 22:10

Kevin Sun