isJwtAboutToExpire static method

bool isJwtAboutToExpire(
  1. String token, {
  2. int bufferInSeconds = 120,
})

Implementation

static bool isJwtAboutToExpire(String token, {int bufferInSeconds = 120}) {
  try {
    // Decode the JWT to get its payload
    final parts = token.split('.');
    if (parts.length != 3) {
      throw Exception("Invalid token format");
    }

    // Base64 decode the payload part of the token
    final payload = json
        .decode(utf8.decode(base64Url.decode(base64Url.normalize(parts[1]))));

    // Check if the `exp` field exists in the payload
    if (!payload.containsKey('exp')) {
      throw Exception("Token does not contain expiration date");
    }

    // `exp` is the expiration time in seconds since epoch
    final exp = payload['exp'] as int;

    // Get the current time in seconds since epoch
    final currentTimeInSeconds =
        DateTime.now().millisecondsSinceEpoch ~/ 1000;

    // Check if the token is about to expire within the buffer period
    return exp - currentTimeInSeconds <= bufferInSeconds;
  } catch (e) {
    Logging.logger.e("Error decoding token: $e", error: e);
    return true; // Assuming it's expired if there's an error
  }
}