checkSessionOnResume method

Future<bool> checkSessionOnResume()

Validates the stored session token when the app returns from background.

Reads SecureStorage and checks AuthTokenModel.isRefreshTokenExpired. If the token is absent or expired, checkSessionOnResume shows auth.sessionExpired itself (via the toast service), then calls _forceLogout to emit AuthenticationStatus.unauthenticated and clear local auth data.

Returns true when the session is valid (no logout triggered). Returns false when the session was invalid and logout was triggered. The return value is informational — callers are allowed to continue with subsequent checks (for example a version check) regardless of the returned value.

Fails-open on unexpected storage errors to avoid silently logging out the user due to a transient OS-level read failure.

Implementation

Future<bool> checkSessionOnResume() async {
  _logger.info('[AuthRepository] checkSessionOnResume: checking stored token');
  try {
    final AuthTokenModel? token = await _secureStorage.getAuthTokenModel();
    if (token == null || token == AuthTokenModel.empty || token.isRefreshTokenExpired) {
      _logger.warn('[AuthRepository] checkSessionOnResume: token absent or expired — forcing logout');
      _toastService.showErrorToast(Strings.authSessionExpired);
      await _forceLogout();
      Sentry.metrics.count('auth.resume.expired', 1);
      return false;
    }
    _logger.info('[AuthRepository] checkSessionOnResume: session valid');
    Sentry.metrics.count('auth.resume.valid', 1);
    return true;
  } catch (e, st) {
    _logger.error('[AuthRepository] checkSessionOnResume: unexpected error', error: e, stackTrace: st);
    return true; // fail-open: do not log out on a transient storage read error
  }
}