- Added the `isExpired` and `expiresSoon` getters to JWT tokens.

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2025-09-17 18:47:28 +02:00
parent 073910546b
commit 04304f106a
5 changed files with 56 additions and 3 deletions
+13 -1
View File
@@ -1,3 +1,15 @@
## 1.4.7
- Added the `isExpired` and `expiresSoon` getters to JWT tokens.
Example:
```dart
String token = "your.jwt.token";
bool isExpired = token.isExpired;
bool expiresSoon = token.expiresSoon;
```
## 1.4.6
- Updated dependencies to the latest versions
@@ -17,7 +29,7 @@
## 1.4.2
- Added the `isLeapYear` extension to the `DateTime` and `int` objects.
- Added the `FixedSizeList` class. See the readme and examples for details.
- Added the `FixedSizeList` class. See the README and examples for details.
## 1.4.1
+7 -1
View File
@@ -267,7 +267,13 @@ Here are some examples of how to use the utilities and extensions provided by th
```dart
String jwt = "your.jwt.token";
// Returns a `DateTime?` when the token expires
final DateTime? email = jwt.jwt.expiryTime;
final DateTime? expiryTime = jwt.jwt.expiryTime;
// Returns `true` if the token has expired
final bool jwt.jwt.isExpired;
// Returns `true` if the token expires within the next (1) minute
final bool jwt.jwt.expiresSoon;
```
- Extracting the user ID (`jwt["uid"]`)
+23
View File
@@ -149,6 +149,29 @@ extension JWTMapUtility on JwtPayload {
}
}
/// Determines whether the JWT has expired.
///
/// If the current `expiryTime` is `null`, this value always returns `true`.
/// Otherwise, the current `expiryTime` is compared to the current `DateTime`.
/// Returns `true` if `DateTime.now()` is after the `expiryTime`. Otherwise,
/// returns `false`.
bool get isExpired {
if (expiryTime == null) return true;
return DateTime.now().isAfter(expiryTime!);
}
/// Determines whether the JWT will expire within the next (1) minute.
///
/// If the current `expiryTime` is `null`, this value always returns `true`.
/// Otherwise, the current `expiryTime` is compared to the current `DateTime`,
/// plus (1) minute..
/// Returns `true` if the token expires within the next (1) minute. Otherwise,
/// returns `false`.
bool get expiresSoon {
if (expiryTime == null) return true;
return DateTime.now().add(const Duration(minutes: 1)).isAfter(expiryTime!);
}
/// Extracts the user ID from the JWT payload.
///
/// This method retrieves the `uid` field, which is typically the unique user identifier.
+1 -1
View File
@@ -1,7 +1,7 @@
name: arcane_helper_utils
description: Provides a variety of helpful utilities and extensions for Flutter
and Dart.
version: 1.4.6
version: 1.4.7
repository: https://github.com/hanskokx/arcane_helper_utils
issue_tracker: https://github.com/hanskokx/arcane_helper_utils/issues
+12
View File
@@ -59,6 +59,18 @@ void main() {
expect(expiry?.year, 2025); // Based on the exp value in the test token
});
test("jwt.isExpired checks expiration on tokens properly", () {
final bool isExpired = validToken.jwt.isExpired;
expect(isExpired, isNotNull);
expect(isExpired, true);
});
test("jwt.expiresSoon checks expiration on tokens properly", () {
final bool expiresSoon = validToken.jwt.expiresSoon;
expect(expiresSoon, isNotNull);
expect(expiresSoon, true);
});
test("jwt.expiryTime throws an exception for invalid token", () {
expect(
() => "invalid.token".jwt.expiryTime,