Skip to content

Commit

Permalink
fix: assert type in decodeJWTPayload (#1018)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?

Bug fix

## What is the current behavior?

Type checking and/or linting does not pass for some configurations, per
#967 and
#1017

## What is the new behavior?

We're now asserting that `parts[1]` is a string (aka isn't undefined),
since we know from a previous check that the array length is 3.

## Additional context

Fixes #967 and fixes #1017

Co-authored-by: Kang Ming <kang.ming1996@gmail.com>
  • Loading branch information
j4w8n and kangmingtay authored Jan 29, 2025
1 parent 510654b commit 3d80039
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ export function decodeJWTPayload(token: string) {
throw new Error('JWT is not valid: not a JWT structure')
}

if (!base64UrlRegex.test(parts[1])) {
if (!base64UrlRegex.test(parts[1] as string)) {
throw new Error('JWT is not valid: payload is not in base64url format')
}

const base64Url = parts[1]
const base64Url = parts[1] as string
return JSON.parse(decodeBase64URL(base64Url))
}

Expand Down

0 comments on commit 3d80039

Please sign in to comment.