isValid property

bool isValid

Loosely checks if the current AuthStore has valid auth data (eg. whether the token is expired or not).

Implementation

bool get isValid {
  final parts = token.split(".");
  if (parts.length != 3) {
    return false;
  }

  final tokenPart = base64.normalize(parts[1]);
  final data = jsonDecode(utf8.decode(base64Decode(tokenPart)))
      as Map<String, dynamic>;

  final exp = data["exp"] is int
      ? data["exp"] as int
      : (int.tryParse(data["exp"].toString()) ?? 0);

  return exp > (DateTime.now().millisecondsSinceEpoch / 1000);
}