Aws-sdk-ios: Authentication for AppSync using User Pools

Created on 3 Apr 2019  路  5Comments  路  Source: aws-amplify/aws-sdk-ios

I have asked this question nearly a year ago and still no reply. I am creating another case and hope it can be resolved.

I want to know how to properly authenticate for my AppSync configuration using user pools

Currently this is in my appDelegate:

let appSyncConfig = try AWSAppSyncClientConfiguration(url: AppSyncEndpointURL, serviceRegion: AppSyncRegion, userPoolsAuthProvider: self, urlSessionConfiguration: urlSessionConfig, s3ObjectManager: transferUtility)

The only part I'm concerned with here is the userPoolsAuthProvider: self. In order for that to work I need to provide the following extension to my appDelegate:

extension AppDelegate: AWSCognitoUserPoolsAuthProvider {

```
func getLatestAuthToken() -> String {
// get and return token

    var token: String?

    pool.currentUser()?.getSession().continueOnSuccessWith(block: { (task) -> Any? in
        token = task.result!.idToken!.tokenString
        return nil
    }).waitUntilFinished()

    return token!
}

}
```

The problem with the above is that firstly if the token is nil then the app crashes. And secondly during offline cases I'm not sure what is happening. Is this using the refresh token to update the id token and what happens when I'm not online.

This mostly works but sometimes it doesn't and my appSync mutations just hang with no callback.

Can you please provide a clear example on how to achieve user Pools authentication for appSync. You promised this nearly a year ago and have not done it yet.

appsync closing-soon-if-no-response mobile client question requesting info

All 5 comments

Hello @alionthego

Apologies for the delay in response. We have updated our APIs to be more friendly and also more communicative when there are error cases.

The best point of reference for the latest SDK would be to refer this link: https://aws-amplify.github.io/docs/ios/api#cognito-user-pools

With AWSMobileClient, we support a bunch of features including more gracious handling of offline cases and expired tokens. You can also chose to queue your requests till the tokens become valid(by renewal) or abandon a request. You can find the documentation related to AWSMobileClient here:

Please let me know if you have any further questions.

Best,
Rohan

Thank you Rohan, I have adopted the AWSMobileClient and it is working well so far. Can you please refer me to how to setup Lambda and S3 to also be configured to use the AWSMobileClient. I couldn't fine that documentation. At the moment my service configuration in AppDelegate is:

// setup logging
        AWSDDLog.sharedInstance.logLevel = .verbose

        // setup service configuration
        let serviceConfiguration = AWSServiceConfiguration(region: CognitoIdentityUserPoolRegion, credentialsProvider: nil)

        // create pool configuration
        let poolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: CognitoIdentityUserPoolAppClientId, clientSecret: CognitoIdentityUserPoolAppClientSecret, poolId: CognitoIdentityUserPoolId)

        // initialize user pool client
        AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: poolConfiguration, forKey: AWSCognitoUserPoolsSignInProviderKey)

        // fetch the user pool client we initialized in above step
        self.pool = AWSCognitoIdentityUserPool(forKey: AWSCognitoUserPoolsSignInProviderKey)

        let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: CognitoIdentityPoolId, identityProviderManager: pool)

        let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
        AWSServiceManager.default().defaultServiceConfiguration = configuration

Do I still need this code or can I now have AWSMobileClient handle the authentication for Lambda and S3

Same question here.

IMHO we need documentation for cases where the setup automatically generated by aws-amplify is not used.

(As an example, see https://github.com/aws-amplify/aws-sdk-ios/issues/1406 )

Hi @alionthego and @thomers

We have some documentation in https://aws-amplify.github.io/docs/ios/manualsetup#direct-aws-service-access that should help you use AWSMobileClient for accessing services like Lambda and S3.

Please let us know if this addresses the issues.

This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

motivus picture motivus  路  4Comments

aTylerRice picture aTylerRice  路  3Comments

minhthuc251 picture minhthuc251  路  4Comments

pawlowskialex picture pawlowskialex  路  4Comments

victorleungtw picture victorleungtw  路  4Comments