Specify type-safe jwtPayload#49
Closed
CharlesFarris wants to merge 5 commits into
Closed
Conversation
CharlesFarris
commented
May 24, 2026
Contributor
- Add helper methods
- Add tests
- Add helper methods - Add tests
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a concrete, shared JWT payload shape (TodoJwtPayload) and a helper to create it, then updates page handlers and tests to rely on jwtPayload.preferred_username directly instead of parsing an unknown payload.
Changes:
- Added
TodoJwtPayloadandcreateTodoJwtPayloadhelper insrc/shared/jwtMiddleware.tswith unit tests. - Updated authenticated pages and related tests to use
c.var.jwtPayload.preferred_usernameand to sign tokens with the new payload shape. - Removed the previous
getUsernameFromJwtPayloadutility (and its tests) that defensively handledunknownpayloads.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/shared/utility.ts | Removes the previous JWT payload parsing helper. |
| src/shared/utility.test.ts | Removes tests for the deleted parsing helper. |
| src/shared/jwtMiddleware.ts | Adds TodoJwtPayload type and createTodoJwtPayload helper. |
| src/shared/jwtMiddleware.test.ts | Adds unit tests for createTodoJwtPayload. |
| src/shared/appVariables.ts | Replaces JwtVariables with an explicit jwtPayload: TodoJwtPayload variable. |
| src/shared/pageJwtMiddleware.test.ts | Updates JWT signing in tests to use the new typed payload helper. |
| src/features/login/loginApi.tsx | Introduces a typed jwtPayload object when signing login tokens. |
| src/features/home/homePage.tsx | Uses c.var.jwtPayload.preferred_username directly. |
| src/features/home/homePage.test.tsx | Updates test JWT signing to the new payload helper. |
| src/features/add-todo/addToDoPage.tsx | Uses c.var.jwtPayload.preferred_username directly. |
| src/features/add-todo/addToDoPage.test.tsx | Updates test JWT signing to the new payload helper. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+9
to
+10
| .get("/", (c) => { | ||
| const username = getUsernameFromJwtPayload(c.var.jwtPayload); | ||
| const username = parseJwtPayload(c.var.jwtPayload).preferred_username; |
Comment on lines
+9
to
+10
| .get("/", (c) => { | ||
| const username = getUsernameFromJwtPayload(c.var.jwtPayload); | ||
|
|
||
| const username = parseJwtPayload(c.var.jwtPayload).preferred_username; |
| logger: Logger; | ||
| } & JwtVariables; | ||
| jwtPayload: unknown; | ||
| validJwtPayload: JwtPayload; |
Comment on lines
+8
to
+13
| const jwtPayloadSchema = z.object({ | ||
| sub: z.string(), | ||
| preferred_username: z.string(), | ||
| role: z.string(), | ||
| iss: z.string(), | ||
| exp: z.number(), |
Comment on lines
68
to
74
| const appConfig = createAppConfig({ | ||
| JWT_SECRET: "12345678901234567890123456789012", | ||
| }); | ||
| const token = await sign({ sub: "1234" }, appConfig.jwt.secret, "HS256"); | ||
| const token = await sign(createJwtPayload(), appConfig.jwt.secret, "HS256"); | ||
| let handlerReached = false; | ||
| const app = new Hono<{ Variables: AppVariables }>() | ||
| .use("*", createAppConfigMiddleware(appConfig)) |
Contributor
Author
|
The branch was abandoned in favor of a different approach. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.