Skip to content

Commit c3a908d

Browse files
committed
refactor: simplify decode method
1 parent a3d2358 commit c3a908d

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

lib/src/jwt.dart

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class JWT {
188188
}
189189

190190
/// Decode a token without checking its signature
191-
///
191+
///
192192
/// This also sets [JWT.audience], [JWT.subject], [JWT.issuer], and
193193
/// [JWT.jwtId] even though they are not verified. Use with caution.
194194
static JWT decode(String token) {
@@ -204,18 +204,19 @@ class JWT {
204204
payload = utf8.decode(base64.decode(base64Padded(parts[1])));
205205
}
206206

207-
if (header == null || header is! Map<String, dynamic>) {
208-
return JWT(payload);
209-
} else {
210-
return JWT(
211-
payload,
212-
header: header,
213-
audience: _parseAud(payload['aud']),
214-
issuer: payload['iss']?.toString(),
215-
subject: payload['sub']?.toString(),
216-
jwtId: payload['jti']?.toString(),
217-
);
218-
}
207+
final audiance = _parseAud(payload['aud']);
208+
final issuer = payload['iss']?.toString();
209+
final subject = payload['sub']?.toString();
210+
final jwtId = payload['jti']?.toString();
211+
212+
return JWT(
213+
payload,
214+
header: header is! Map<String, dynamic> ? null : header,
215+
audience: audiance,
216+
issuer: issuer,
217+
subject: subject,
218+
jwtId: jwtId,
219+
);
219220
} catch (ex, stackTrace) {
220221
if (ex is Exception && ex is! JWTException) {
221222
throw JWTUndefinedException(ex, stackTrace);

0 commit comments

Comments
 (0)