isAuthTokenValid method

  1. @override
Future<bool> isAuthTokenValid(
  1. String authToken
)
override

Implementation

@override
Future<bool> isAuthTokenValid(String authToken) async {
  try {
    final parts = authToken.split('.');
    if (parts.length != 3) {
      return false;
    }
    String encodedPayload = parts[1];
    int requiredPadding = 4 - encodedPayload.length % 4;
    if (requiredPadding < 4) {
      encodedPayload += '=' * requiredPadding;
    }
    final payload = utf8.decode(base64Url.decode(encodedPayload));
    final Map<String, dynamic> data = jsonDecode(payload);
    if (data.containsKey('exp')) {
      final int expirationTime = data['exp'] * 1000;
      final int currentTime = DateTime.now().millisecondsSinceEpoch;
      return expirationTime > currentTime;
    }
    return false;
  } catch (e) {
    return false;
  }
}