amazon-cognito-identity-dart-2
amazon-cognito-identity-dart-2 copied to clipboard
Persisting user session with FlutterSecureStorage?
Hi, I'm a junior who's a bit lost when it comes to persisting session. I've read all the existing issues regarding session persistence for this repo but I'm still confused as to what route to go, as there seems to be several choices available:
- the
await cognitoUser.cacheTokens();
method, that I'm not sure how to use when I want to check if a session is still valid - the example provided in the readme.md talks about extending your custom storage with the abstract class CognitoStorage
- this example uses SharedPrefs
If I am mixing things up, or am in the complete wrong, please feel free to correct me.
I would like to go with FlutterSecureStorage, therefore which of these 3 routes mentioned above should I base my code upon? Below is my own code that I use to securely store the id and refresh tokens on login, using said package:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
class SecureStorage {
final FlutterSecureStorage _storage = const FlutterSecureStorage();
final keyIdToken = 'idtoken';
final keyRefreshToken = 'refreshtoken';
Future<void> setIdToken(String? idToken) async {
await _storage.write(key: keyIdToken, value: idToken);
}
Future<String?> getIdToken() async {
var data = await _storage.read(key: keyIdToken);
return data;
}
Future<void> setRefreshToken(String? refreshToken) async {
await _storage.write(key: keyRefreshToken, value: refreshToken);
}
Future<String?> getRefreshToken() async {
var data = await _storage.read(key: keyRefreshToken);
return data;
}
}
This is up to you. You can use your SecureStorage or rewrite it smth like
class SecureStorage imlements/extends CognitoStorage {...}
and pass it into the CognitoUserPool
as in examples.
In this case, you can use these methods
https://github.com/furaiev/amazon-cognito-identity-dart-2/blob/main/lib/src/cognito_user.dart#L1032-L1090
and pass it into the
CognitoUserPool
as in examples.
By examples here, which ones do you specifically refer to?
(PS: Sorry for closing and reopening, I fat fingered the comment button)
Like in readme - https://github.com/furaiev/amazon-cognito-identity-dart-2#use-custom-storage
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.