amazon-archives / amazon-cognito-js

Amazon Cognito Sync Manager for JavaScript
http://aws.amazon.com/cognito
Apache License 2.0
200 stars 83 forks source link

Unable to retrieve values from dataSet if the app starts in offline mode #30

Closed sergiomilici closed 7 years ago

sergiomilici commented 8 years ago

I'm trying to make my web app work offline. I have done the registration and login using aws cognito. I created a CognitoSyncManager and then a DataSet ( basically following the examples ), and i was able to save things and retrieve them. My problem is i can't get values from the dataSet if i reload my app been offline. I'm getting "network error" ( while authenticating ) because i'm not online. I tried to access the dataset, i can ask it for a value, but they are all "undefined". So, is it possible to access it been offline since the app starts?

johnborges commented 7 years ago

I can confirm the same issue. I'm using this lib in conjunction with a Cordova/PhoneGap based app. It seems that dataset.put works but getting values dataset.get always returns an undefined dataset while offline.

aws-sdk-js@2.7.1 amazon-cognito-js@1.0.0 cordova-ios@4.3.0

johnborges commented 7 years ago

I think i found part of the problem. It seems like AWS.config.credentials.identityId is a widely used parameter for many of the CognitoSyncLocalStorage methods. Issue I found and reported earlier sets AWS.config.credentials.identityId to null when offline. As a test I hardcoded my identityId while offline and dataset.get() works.

AWS.config.region = 'us-east-1';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityId:"hardcoded_id"
});

AWS.config.credentials.get(function(err){
    // at this point AWS.config.credentials.identityId --> null
    // below i reset it
    AWS.config.credentials.identityId = "hardcoded_id";

    syncClient = new AWS.CognitoSyncManager();
    syncClient.openOrCreateDataset('profile', function(err, dataset) {
        dataset.get('myRecord', function(err, value) {
            // works now when offline
        });

    });
);
PaolinoAngeletti commented 7 years ago

Excuse me if I enter into the discussion, but I have a doubt about Cognito and offline. The question is simple yet complex: You can log in to Cognito offline? Or is it only possible to save data locally to allow operation of the application offline?

I ask this because I would use my application especially in offline, so I have to make a login without a connection.

I hope that I have explained well. Thansk for help

johnborges commented 7 years ago

@PaolinoAngeletti I'm not exactly sure what you mean by "log in to Cognito". Currently, there is a bug with accessing Cognito Sync datasets offline. The bug exists in aws-sdk-js, not amazon-cognito-js.

My understanding of the this lib is that it allows you work with Cognito Sync datasets using LocalStorage whether you're online or offline. Syncing obviously requires connectivity but getting and setting data can be done regardless.

I've created a bug in aws-sdk-js since I believe this is where the problems occurs when trying to access data offline from Cognito. I've tested the fix and it has worked for me. Whenever the fix gets pulled into master you should update aws-sdk-js.

PaolinoAngeletti commented 7 years ago

I'll explain to be more clear: I'm developing an Android app which provides a login screen with username and password. When there is connection, an HTTP request is made to verify the username and password entered on Cognito.

My question was whether you can verify your username and password (always on Cognito) even when the connection is absent. Why else in my app you can not make a login without connection on Cognito, and then I will have to implement other roads.

More probabily the answer is negative, but my idea is this:

`

            if(haveInternetConnection(context))
            {

                // if i have connection, i proceed to default and without problems
                username = ViewHelper.getStringValue(activity, EDIT_TEXT_USERNAME_ID);
                password = ViewHelper.getStringValue(activity, EDIT_TEXT_PASSWORD_ID);

                final CognitoUser cognitoUser = cognitoUserPool.getUser(username);

                cognitoUser.getSessionInBackground(authenticationHandler);

            }
            else
            {

                // if i not have connection, i want access to cognito with data saved locally. 
               // it's possible?
                CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                        context,
                        "XXX", // Identity Pool ID
                        Regions.XX // Region
                );

                // Initialize the Cognito Sync client
                CognitoSyncManager syncClient = new CognitoSyncManager(
                        context,
                        Regions.XX, // Region
                        credentialsProvider);

                Dataset dataset = syncClient.openOrCreateDataset("myDataset");
                String user = dataset.get("username"); 
                String password = dataset.get("password");

                // here the code for log in to cognito
            }
        }`

Thank you for your previous answer.

johnborges commented 7 years ago

@PaolinoAngeletti I would not recommend not storing usernames & passwords in plaintext using Cognito Data Sets. Cognito has support for federated identity providers like Google, Facebook so you don't need to recreate your own log in service. Read the docs before you get started. By the looks of your code you're using Java. This thread is for issues pertaining to amazon-cognito-js.

itrestian commented 7 years ago

@johnborges Does the fix you mentioned in https://github.com/aws/amazon-cognito-js/issues/17 also fix this?

johnborges commented 7 years ago

@itrestian From my tests, yes.

itrestian commented 7 years ago

Thanks!

sergiomilici commented 7 years ago

Thanks everyone!!!