getSessionByUserId method
Retrieves a session by userId, optionally forcing a fetch from the server.
Implementation
Future<AuthUserSession?> getSessionByUserId(
String userId, {
bool force = false,
}) async {
try {
if (userId.isEmpty) return null;
if (force) {
logger.info(
this,
'Force fetching session for user $userId from server.',
);
var responseSession = await sessionService.getSession(userId);
var session = responseSession != null
? AuthUserSession.mapFromDto(dto: responseSession)
: null;
await validateSession(session);
await _localSessionRepository.saveSession(session!);
return session;
}
// Use Repo to check memory/disk
var session = await _localSessionRepository.getSession(userId);
if (session != null) {
session = await refreshSessionIfExpiring(session);
return session;
}
// Fetch from server if not in repo
logger.info(this, 'Fetched session for user $userId from server.');
var responseSession = await sessionService.getSession(userId);
session = responseSession != null
? AuthUserSession.mapFromDto(dto: responseSession)
: null;
await validateSession(session);
await _localSessionRepository.saveSession(session!);
return session;
} catch (e) {
logger.error(
this,
'Error retrieving session for user $userId: $e',
error: e,
);
return null;
}
}