Bernard
Bernard

Reputation: 4580

How to use cognito to authenticate user by using mobile hub?

I'm trying to use Mobile Hub for first time. I have created a sample app and user sign in only through facebook. My main problem is

  1. How to find out this is the first time user login or existed before
  2. How to sync data to AWS cognito dataset

To solve first issue I have come up with this solution to check user dataset if it is empty then this is first time user joined.

In order to do that in app delegate didFinishLaunchingWithOptions:

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc]
                                                      initWithRegionType:AWSRegionUSEast1
                                                      identityPoolId:@"us-east-1:pool id is here"];

AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];



[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

And later I'm trying to do this:

AWSCognito *syncClient = [AWSCognito defaultCognito];

// Create a record in a dataset and synchronize with the server
AWSCognitoDataset *dataset = [syncClient openOrCreateDataset:@"Sample"];
[dataset setString:@"test2" forKey:@"test"];
[[dataset synchronize] continueWithBlock:^id(AWSTask *task) {
    // Your handler code here
    return nil;
}];

[dataset synchronize];

There is no dataset for the user on the server. I'm trying to create this dataset. But the problem is [AWSCognito defaultCognito] return null. I checked documentation and it says:

Returns the singleton service client. If the singleton object does not exist, the SDK instantiates the default service client with defaultServiceConfiguration from [AWSServiceManager defaultServiceManager]. The reference to this object is maintained by the SDK, and you do not need to retain it manually. Returns nil if the credentials provider is not an instance of AWSCognitoCredentials provider.

I'm not sure what I am missing. Why when I call this singleton it returns null!

How can I check if the credentials provider is an instance of AWSCognitoCredentials provider?

I checked in identity browser in Cognito services. New Identity is created but there is no dataset. can it be something related to roles and IAM?

Upvotes: 0

Views: 163

Answers (1)

Andrew C
Andrew C

Reputation: 406

You should follow the example of the AWS Mobile Hub sample mobile app project, which you can download from the "Integrate" page in Mobile Hub. Assuming you have enabled the "User Data Storage" feature in your Mobile Hub project, that sample includes all the required Info.plist file entries, which will enable Amazon Cognito. You should also read through all the instructions for integration in the "Integrate" page of the AWS Mobile Hub console, to make sure you're not missing any other integration steps.

Upvotes: 1

Related Questions