jwt_decoder 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 82

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.

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
}

[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

example/example.dart

import 'package:jwt_decoder/jwt_decoder.dart';

main() {
  String token =
      "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikd1c3Rhdm8iLCJpYXQiOjE1MTYyMzkwMjIsImV4cCI6NDczNDYxNTg1OH0.hh-TTBPS8z-UxdmfXWn7AwW2y_Lq3aPnlIQdqV2KEC4";

  // decode() method will decode your token's payload
  Map<String, dynamic> decodedToken = JwtDecoder.decode(token);
  // Now you can use your decoded token
  print(decodedToken["name"]);

  // isExpired() method will tell you if your token is already expired or not.
  // An useful method when you have to handle sessions and you want the user
  // to authenticate if has an expired token
  bool isTokenExpired = JwtDecoder.isExpired(token);

  if (!isTokenExpired) {
    // The user should authenticate
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  jwt_decoder: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:jwt_decoder/jwt_decoder.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
73
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • jwt_decoder that is a package requiring null.

Health issues and suggestions

Document public APIs. (-1 points)

5 out of 5 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test