FLUTTER ECOSYSTEM

gustavo0197/dart-jwt-decoder

適用於 Dart/Flutter 的 JSON Web Token 解碼器

dart-jwt-decoder 專案封面
Stars
61
Forks
24
最近推送(UTC)
2024年9月10日
專案狀態
未封存
Gustavo Velazquez GitHub avatar
GITHUB User

Gustavo Velazquez ↗

@GensoftMars
語言Dart

此儲存庫發佈的套件

使用的依賴

依賴清單 1 項
  • test開發依賴^1.25.8

原始 README

以下為英文專案原文快照,最新內容請造訪 GitHub。

展開 / 收合專案 README

JWT Decoder

This is a small library for decoding a json web token for dart / flutter. Since the header and payload is base64 encoded you can easily know the stored data with no password, you can also know if the token is expired or not.

If you like this library there's a version for Vue here. And for React here

Getting Started

Decode a token
main () {
  String yourToken = "Your JWT";
  Map<String, dynamic> decodedToken = JwtDecoder.decode(yourToken);

  /*
  If the token has a valid format, you will get a Map<String, dynamic>
  Your decoded token can look like:
  {
     "sub": "1234567890",
     "name": "Gustavo",
     "iat": 1516239022,
     "exp": 1516239022,
     "randomKey": "something else"
  }
  */
}
Know if the token is expired
main () {
  String yourToken = "Your JWT";
  bool hasExpired = JwtDecoder.isExpired(yourToken);

  // You will get a true / false response
  // true: if the token is already expired
  // false: if the token is not expired
}
Get expiration date
main () {
  String yourToken = "Your JWT";
  DateTime expirationDate = JwtDecoder.getExpirationDate(token);

  // 2025-01-13 13:04:18.000
  print(expirationDate);
}
You can know how old your token is
// Token payload must include an 'iat' field
main () {
  String yourToken = "Your JWT";
  Duration tokenTime = JwtDecoder.getTokenTime(token);

  // 15
  print(tokenTime.inDays);
}

License

MIT