furaiev / amazon-cognito-identity-dart-2

Unofficial Amazon Cognito Identity Provider Dart SDK, to easily add user sign-up and sign-in to your mobile and web apps with AWS.
MIT License
187 stars 114 forks source link

Data not being persisted? #75

Closed permadart closed 4 years ago

permadart commented 4 years ago

So I created a file with static variables related to cognito like this:

static final customStore = CustomStorage('custom:');

  static final CognitoUserPool userPool = CognitoUserPool(
    'us-east-1_d2KN0SE2Z',
    '4p6jn62gtbthos9eol0tgcmpla',
    storage: customStore,
  );

  static CognitoUser cognitoUser;

  static CognitoUserSession session;

  static AuthenticationDetails authenticationDetails; 

And then I use it to login like this

GP.cognitoUser = CognitoUser(
        email,
        GP.userPool,
        storage: GP.customStore,
      );

      GP.authenticationDetails = AuthenticationDetails(
        username: email,
        password: pwd,
      );

      try {
        await GP.cognitoUser.authenticateUser(
          GP.authenticationDetails,
        );
      } on CognitoClientException catch (e) {
        if (e.code == 'UserNotConfirmedException') {
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => ConfirmationScreen(
                email: email,
              ),
            ),
          );
        } else {
          rethrow;
        }
      }

      GP.cognitoUser = await GP.userPool.getCurrentUser();
      GP.session = await GP.cognitoUser.getSession();

Works very good. But I was under the understanding that this

Map<String, String> _storage = {};

class CustomStorage extends CognitoStorage {
  String prefix;

  CustomStorage(this.prefix);

  @override
  Future setItem(String key, value) async {
    _storage[prefix + key] = json.encode(value);
    return _storage[prefix + key];
  }

  @override
  Future getItem(String key) async {
    if (_storage[prefix + key] != null) {
      return json.decode(_storage[prefix + key]);
    }
    return null;
  }

  @override
  Future removeItem(String key) async {
    return _storage.remove(prefix + key);
  }

  @override
  Future<void> clear() async {
    _storage = {};
  }
}

Was supposed to persist the data, but when I close the app and reopen it immediately or at any given time, the session I'm searching for in here

Future<bool> doFindUserSession() async {
    GP.cognitoUser = await GP.userPool?.getCurrentUser();
    GP.session = await GP.cognitoUser?.getSession();

    if (GP.session?.isValid() ?? false) {
      isAuthenticated = true;

      notifyListeners();

      return true;
    } else {
      return false;
    }
  }

Is nowhere to be found.

Am I missing something here? Thanks

furaiev commented 4 years ago

Is your GP a singletone? It seems like you call 2 different instances.

thisishardik commented 4 years ago

Give solutions pls

permadart commented 4 years ago

@furaiev You are correct, I wasn't making sure it was a singleton. Thanks!