Comments (3)
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:
- Initiate CognitoUserPool object with Cognito data (user pool ID and client ID).
- Use getCurrentUser method on initiated CognitoUserPool object to acquire cached user (if return null, no user was cached).
- Use getSession method on user object to aquire session.
from amazon-cognito-identity-dart-2.
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.
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)
- SSO w/ Flutter AWS Cognito HOT 2
- zip file broken HOT 2
- use case 19 HOT 2
- Unauthorized/Anonymus user token HOT 2
- Forgot Password HOT 2
- Melos version bump? HOT 5
- [ASK] Is it possible to getSession with only `refreshToken` value ? HOT 2
- Use case 19 HOT 3
- CognitoUserDeviceConfirmationNecessaryException is not being thrown HOT 3
- Enabling TOTP for MFA HOT 5
- how to solve custom challenge? HOT 3
- Federated Login... Refresh Access Token HOT 2
- AuthenticationDetails.validationData not transmitted HOT 5
- VerifyDevice request fails if username used in sendMFA() is email or phone number HOT 2
- MFA_SETUP challenge when user pool has "required MFA" HOT 3
- sendMFACode method requires SECRET_HASH HOT 2
- How to verify Password Only without session? HOT 2
- "Missing required parameter Session" while MFA is required HOT 6
- Mismatched key prefix when caching and restoring device HOT 7
- Issue when UserConfirmationNecessary is true HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from amazon-cognito-identity-dart-2.