amazon-cognito-identity-dart-2 icon indicating copy to clipboard operation
amazon-cognito-identity-dart-2 copied to clipboard

Persisting user session with FlutterSecureStorage?

Open mailliwi opened this issue 2 years ago • 3 comments

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;
  }
}

mailliwi avatar Jul 05 '22 15:07 mailliwi

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

furaiev avatar Jul 05 '22 16:07 furaiev

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)

mailliwi avatar Jul 06 '22 00:07 mailliwi

Like in readme - https://github.com/furaiev/amazon-cognito-identity-dart-2#use-custom-storage

furaiev avatar Jul 06 '22 07:07 furaiev

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Jul 10 '23 02:07 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jul 25 '23 02:07 github-actions[bot]