getSession method

Future<AuthUserSession?> getSession(
  1. String userId
)

Tries to find a session in memory first, then disk. If found on disk, it automatically adds it to memory and updates the index.

Implementation

Future<AuthUserSession?> getSession(String userId) async {
  if (userId.isEmpty) return null;

  // Check Memory
  var session = _memorySessions.firstWhereOrNull((s) => s.userId == userId);
  if (session != null) return session;

  // Check Storage
  try {
    var storedJson = await _storage?.readJson(_userSessionKey(userId));
    if (storedJson != null) {
      var storedSession = AuthUserSession.fromJson(storedJson);

      // Add to memory and ensure index is consistent
      _memorySessions.add(storedSession);
      await _updateIndex(userId, add: true);

      return storedSession;
    }
  } catch (e) {
    logger.warning(this, 'Corrupt session data for $userId. Cleaning up.');
    await deleteSession(userId);
  }

  return null;
}