verifyToken static method
Verifies an encrypted token and checks its expiration status.
Implementation
static Future<Token> verifyToken(String? token, {String? key}) async {
try {
if (_isNullOrEmpty(token)) {
return Token(false, "Invalid token (empty)", null);
}
final decryptedPayload = await Encrypter.decryptAES256CBC(
token,
key: key,
);
if (_isNullOrEmpty(decryptedPayload)) {
return Token(false, "Invalid token (decryption failed)", null);
}
final tokenPayload = decryptedPayload as Map<String, dynamic>;
if (tokenPayload.containsKey('exp') && tokenPayload['exp'] != null) {
final expirationTimestamp = tokenPayload['exp'] as int;
final currentTimestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
if (expirationTimestamp < currentTimestamp) {
return Token(false, "Token expired", null);
}
}
return Token(true, "Valid token", tokenPayload['payload']);
} catch (e) {
rethrow;
}
}