jonasroussel / dart_jsonwebtoken

A dart implementation of the famous javascript library 'jsonwebtoken'
MIT License
87 stars 29 forks source link

please, update the `JWT.decode` function for null safety? #31

Closed SittiphanSittisak closed 1 year ago

SittiphanSittisak commented 1 year ago

At the moment. We have a ? type like String?. The JWT.decode must use the try/catch to catch the error.

main () {
  String stringToken = '...';
  late final JWT? jwt;
  try {
    jwt = JWT.decode(stringToken);
  } catch (e) {
    jwt = null;
  }
  if(jwt != null) print(jwt.payload);
}

Please, add JWT.tryDecode to get the JWT? type.

main() {
  String stringToken = '...';
  final JWT? jwt = JWT.tryDecode(stringToken);
  if(jwt != null) print(jwt.payload);
}

Maybe for the JWT.decode too. I don't know are any people love this because decoding the null encode will give the result a null like error but I think this is easy to read. please, update this if your team agrees with this.

jonasroussel commented 1 year ago

I work alone on this package, I only have time during the weekend.

It's a good idea, and it's not hard to do. Added in the last version.

Published v2.6.1: https://pub.dev/packages/dart_jsonwebtoken/versions/2.6.1

SittiphanSittisak commented 1 year ago

@jonasroussel Thank you so much 🦦.