jonasroussel / dart_jsonwebtoken

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

Where the function to read the payload after encode? #28

Closed SittiphanSittisak closed 1 year ago

SittiphanSittisak commented 1 year ago

After encoding in the dart server, the token was sent to the flutter app. Where is the function to read payload from this token in the flutter app? (Or I must create custom function or use a package ?)

yuriboeira11tx commented 1 year ago

I used this way to use token coming from server

try {
  final jwt = JWT.verify(
    response.data,
    SecretKey("YOUR_SECRET_KEY"),
  );

  log('Payload: ${jwt.payload}');

  return jwt.payload; <---- HERE GET YOUR PAYLOAD FROM JWT
} on JWTExpiredError {
  log('jwt expired');
} on JWTError catch (ex) {
  log(ex.message);
}
SittiphanSittisak commented 1 year ago

Hi @yuriboeira11tx . I don't want to store the secret key on the Flutter app. I think this is not secure.

jonasroussel commented 1 year ago

Hello @SittiphanSittisak,

I just added a new function JWT.decode in this new version (https://pub.dev/packages/dart_jsonwebtoken/versions/2.6.0)

Hopefully this will meet your needs

SittiphanSittisak commented 1 year ago

Hi @jonasroussel, thank you so much 🦦 This makes code beautiful and there is no need to use more packages.

(
          ? JWT.decode(jwt)
          : JWT.verify(jwt, SecretKey(secretKey))
).payload;