MaxB
MaxB

Reputation: 215

How do I integrate cognito user pool with identity pool?

I'm practicing with the CognitoUserPoolsSample iOS Obj-C app and trying to add integration with Cognito Identity. I've set up a user pool and an identity pool with the user pool set up as an authentication provider for the identity pool. The user pool is working fine, but the users are not showing up in the identity pool. Here's what I have in applicationDidFinishLaunchingWithOptions:

//setup service config
AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil];

//Configure user pool
AWSCognitoIdentityUserPoolConfiguration *userPoolConfiguration = [[AWSCognitoIdentityUserPoolConfiguration alloc] initWithClientId:@"CLIENT_ID"  clientSecret:@"CLIENT_SECRET" poolId:@"POOL_ID"];
[AWSCognitoIdentityUserPool registerCognitoIdentityUserPoolWithConfiguration:serviceConfiguration userPoolConfiguration:userPoolConfiguration forKey:@"UserPool"];
AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];

//configure identity pool
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc]
                                                      initWithRegionType:AWSRegionUSEast1
                                                      identityPoolId:@"IDENTITY_POOL_ID"
                                                      identityProviderManager:pool];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

Is this correct so far? What is the next step from here? How come when I sign up a new user it doesn't show up in the identity pool? The identity pool console shows zero identities created.

Upvotes: 2

Views: 3459

Answers (2)

MagicFlow
MagicFlow

Reputation: 477

Hi The key thing here to understand is that when you call:

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc]
                                                      initWithRegionType:AWSRegionUSEast1
                                                      identityPoolId:@"IDENTITY_POOL_ID"
                                                      identityProviderManager:pool];

The AWS framework will set everything up for you, and the cognito User Pool and integration with federated identity will work seamelessyly.

A key note which I initally overlooked is here: http://docs.aws.amazon.com/cognito/latest/developerguide/getting-credentials.html

[[credentialsProvider getIdentityId] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    }
else {
       // the task result will contain the identity id
       NSString *cognitoId = task.result;
   }
return nil;
}];

Which forces a refresh of your credentials from the server. Objects contained on the user and also the session can be used to confirm the login and associated cognito id, and sessions tokens.

Be careful not to also use MobileHubHelper with the above code. As the mobile HUB Helper will destroy all of that.

Upvotes: 1

Chetan Mehta
Chetan Mehta

Reputation: 5661

You need to supply the token from Cognito user pools to the Cognito federated identity service. This is exactly how you would integrate Facebook or Google or any other provider with Federated Identity service.

This dev guide page and blog post go over this process in details.

Upvotes: 1

Related Questions