Code Monkey home page Code Monkey logo

Comments (3)

Kacpur avatar Kacpur commented on September 21, 2024 1

To cache user and authentication tokens you must create custom storage and pass it to CognitoUserPool object, when you initiate this. After you successfully authenticate all tokens will be automatically saved to this storage.
Then to restore data try this steps:

  1. Initiate CognitoUserPool object with Cognito data (user pool ID and client ID).
  2. Use getCurrentUser method on initiated CognitoUserPool object to acquire cached user (if return null, no user was cached).
  3. Use getSession method on user object to aquire session.

from amazon-cognito-identity-dart-2.

LudySu avatar LudySu commented on September 21, 2024 1

Hi, not sure what's the problem you are having, but I am following the example code here and have no trouble keeping my logged in state.

I check the result of bool loggedIn = await userService.init(), if it's true then the session is still valid, keep going; if false, do another userService.login(). Also, userService.init() itself handles session refresh automatically, you can see there's a call to cognitoUser.getSession() inside.

Though not answering your question, but hope it helps you.

from amazon-cognito-identity-dart-2.

 avatar commented on September 21, 2024

Ok, i've more or less understood it and tried but it throws an exception

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Exception: Local storage is missing an ID Token, Please authenticate

So lets start with my code. I have a similar class of the example CognitoService where i have my attributes for CognitoUserPool, CognitoUser ecc... and also the next methods.

Future<void> setStorage() async {
    print("Imposto lo storage");
    final prefs = await SharedPreferences.getInstance();
    final storage = Storage(prefs);
    userPool.storage = storage;
  }


  Future<bool> updateCognitoUserWithCurrent() async{  
    cognitoUser = await userPool.getCurrentUser();
    if (cognitoUser == null) {
      return false;
    }
    session = await cognitoUser.getSession();
    return session.isValid();
  }

I have also the constructor methods for creating the CognitoUserPool and the CognitoUser.
I have also the function for login, register ecc... that i found in the use case showed inside the read me document.

So i can register and login and all goes clear, but when I close the application e go back to the login page where it should check if i logged before it throws me the exception.

Before logging I instantiate the object in this way (CognitoSarto is my CognitoService class)

_cognitoClass = new CognitoSarto(email: email, pwd: pwd);
 _cognitoClass.setStorage();

and then i use the login taken from the readme files.

When i'm trying to gain the old logged user I istantiate in this way

  _cognitoClass = new CognitoSarto.fromCached();
     _cognitoClass.setStorage();
     _cognitoClass.updateCognitoUserWithCurrent();`

And the CognitoSarto.fromCached is just a constructor:

CognitoSarto.fromCached() :  super(){
    print("Costruttore CognitoSarto.fromCached");
    userPool = new CognitoUserPool(
        CognitoClass.datiPoolSarto[0], CognitoClass.datiPoolSarto[1],
    );
    super.tipoAccount = CognitoClass.SARTO;
    setSharedPreferences(CognitoClass.SARTO);
  }

Crearly something is wrong with my storage, but the storage is the class that i took from the exampe

class Storage extends CognitoStorage {
  SharedPreferences _prefs;
  int tipoAccount;
  Storage(this._prefs);

  @override
  Future getItem(String key) async {
    String item;
    try {
      item = json.decode(_prefs.getString(key));
    } catch (e) {
      return null;
    }
    return item;
  }

  @override
  Future setItem(String key, value) async {
    await _prefs.setString(key, json.encode(value));
    return getItem(key);
  }

  @override
  Future removeItem(String key) async {
    final item = getItem(key);
    if (item != null) {
      await _prefs.remove(key);
      return item;
    }
    return null;
  }

  @override
  Future<void> clear() async {
    await _prefs.clear();
  }
}

More precisesly the exception is thrown from the

session = await cognitoUser.getSession();

that is inside the updateCognitoUserWithCurrent() method

from amazon-cognito-identity-dart-2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.