jwt_decode 0.3.1 copy "jwt_decode: ^0.3.1" to clipboard
jwt_decode: ^0.3.1 copied to clipboard

A easy dart package to decode JSON Web Tokens, and to check and give its expiry dates

example/example.dart

import 'package:jwt_decode/jwt_decode.dart';

main() {
  // Sample token
  String token =
      'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJKV1QgRGVjb2RlIiwiaWF0IjoxNjA4NTgxNzczLCJleHAiOjE2NDAxMTc3NzMsImF1ZCI6Ind3dy5qd3RkZWNvZGUuY29tIiwic3ViIjoiQSBzYW1wbGUgSldUIiwibmFtZSI6IlZhcnVuIFMgQXRocmV5YSIsImVtYWlsIjoidmFydW4uc2F0aHJleWFAZ21haWwuY29tIiwicm9sZSI6IkRldmVsb3BlciJ9.vXE9ogUeMMsOTz2XQYHxE2hihVKyyxrhi_qfhJXamPQ';

  // To decode the token
  Map<String, dynamic> payload = Jwt.parseJwt(token);
  // Print the payload
  print(payload);

  // Print one of its property(example: email):
  print(payload['email']);

//   To get expiry date
//   Note: The return value from getExpiryDate function is nullable.
  DateTime? expiryDate = Jwt.getExpiryDate(token);
//   Print the expiry date
  print(expiryDate);

  // To check if token is expired
  bool isExpired = Jwt.isExpired(token);
  print(isExpired);

  // Can be used for auth state
  if (!isExpired) {
    //   Token isn't expired
  } else {
    //   Token is expired
  }
}
116
likes
150
points
212k
downloads

Publisher

unverified uploader

Weekly Downloads

A easy dart package to decode JSON Web Tokens, and to check and give its expiry dates

Repository (GitHub)
View/report issues
Contributing

Documentation

API reference

License

BSD-3-Clause (license)

More

Packages that depend on jwt_decode