hasValidToken method

bool hasValidToken({
  1. Duration tolerance = const Duration(seconds: 60),
  2. DateTime? currentTime,
})

Returns true when the participant token is valid (not expired and past its not-before time).

tolerance allows treating tokens as expired ahead of their actual expiry to avoid edge cases. currentTime is primarily intended for testing; it defaults to the current system time.

Implementation

bool hasValidToken({Duration tolerance = const Duration(seconds: 60), DateTime? currentTime}) {
  final payload = jwtPayload;
  if (payload == null) {
    return false;
  }

  final nowUtc = (currentTime ?? DateTime.timestamp()).toUtc();

  final notBefore = payload.notBefore;
  if (notBefore != null && nowUtc.isBefore(notBefore)) {
    return false;
  }

  final expiresAt = payload.expiresAt;
  if (expiresAt == null) {
    return false;
  }

  final comparisonInstant = nowUtc.add(tolerance);
  if (!expiresAt.isAfter(comparisonInstant)) {
    return false;
  }

  return true;
}