decodeJwtPayload function

Map<String, dynamic>? decodeJwtPayload(
  1. String token
)

The payload of a JWT, without checking the signature (that is the server's job). Null when token is not a JWT.

Implementation

Map<String, dynamic>? decodeJwtPayload(String token) {
  final parts = token.split('.');
  if (parts.length != 3) return null;
  try {
    final json = utf8.decode(base64Url.decode(base64Url.normalize(parts[1])));
    final payload = jsonDecode(json);
    return payload is Map<String, dynamic> ? payload : null;
  } on FormatException {
    return null;
  }
}