decodeJwt function
Implementation
Map decodeJwt(String jwt) {
List<String> parts = jwt.split('.');
if (parts.length == 5) {
throw 'Only JWTs using Compact JWS serialization can be decoded';
}
if (parts.length != 3) {
throw 'Invalid JWT';
}
String payload = parts[1];
if (payload.isEmpty) {
throw 'JWTs must contain a payload';
}
Uint8List decoded;
try {
decoded = base64Url.decode(base64Url.normalize(payload));
} catch (_) {
throw 'Failed to base64url decode the payload';
}
try {
String jsonPayload = utf8.decode(decoded);
Map<String, dynamic> result = jsonDecode(jsonPayload);
return result;
} catch (e) {
throw 'Failed to decode JWT: $e';
}
}