jwt_decoder

v2.0.1

This small library helps you to decode Json Web Tokens, you can also know if a JWT is already expired.

Архив пакета: https://pubdev.letsnova.ru/api/archives/jwt_decoder/2.0.1.tar.gz

Установкаdart pub add jwt_decoder

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

История изменений

[1.0.0] - decode() and isExpired() methods

  • decode method will decode your token's payload and return a Map<String, dynamic>
  • isExpired method is used to know if a token is already expired or not

[1.0.1] - Example file

  • An example file was added

[1.0.2] - License was added

[1.0.3] - getExpirationDate() and getTokenTime() methods

  • getExpirationDate() returns the expiration date of the token
  • getTokenTime() you can use this method to know how old your token is

[1.0.4] - Minor improvements

  • isExpired() and getExpirationDate() the expiration time is converted to int

[2.0.0] - Migration to null safety

  • Updated SDK dependency version to 2.12.0+
  • Avoid all null returns and replaced them with FormatException throws when tokens are not valid. This is a breaking change as users expecting null returns will now get an exception instead.
  • Added JwtDecoder methods documentation.
  • Replaced flutter framework dependencies to Dart only dependency.

[2.0.1] - getRemainingTime() method