Skip to content

Add validation and error handling to parseJwt function in auth.ts #1782

@coderabbitai

Description

@coderabbitai

Description

The parseJwt function in zmscitizenview/src/utils/auth.ts currently lacks defensive checks and proper error handling, which can lead to uncaught errors when processing malformed tokens.

Problems

  1. No token structure validation: token.split(".")[1] will be undefined for malformed tokens, causing .replace() to fail with a cryptic error instead of a clear validation failure.

  2. Missing base64url padding handling: atob() throws on improperly padded input, which some JWT payloads require. The function doesn't add proper padding before decoding.

  3. No error context: JSON.parse() and atob() errors propagate without context, making debugging difficult for consumers of this function.

Expected Behavior

The function should:

  • Validate the token structure before processing
  • Handle base64url padding correctly
  • Provide clear error messages with context when parsing fails

Related Links

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions