refactor(auth): type jwt user payload and drop any casts (#541)#560
Conversation
|
@ramnnn2006 is attempting to deploy a commit to the Prashantkumar Khatri's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @ramnnn2006, Thanks for opening this pull request. This PR has been automatically classified based on the files modified. Applied Labels
Primary Review Area
Reviewer@Harxhit has been identified as the primary reviewer for this pull request. If you have any questions regarding the affected area or implementation details, feel free to reach out to the assigned reviewer. Thank you for your contribution! |
CI — All Checks PassedBackend — PASS
Mobile — SKIP
Web — SKIP
Last updated: |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR improves Fastify JWT/auth type safety by introducing a typed AuthenticatedUser, adding Fastify module augmentations for JWT user and an authenticate decorator, and removing any casts from auth routes.
Changes:
- Added
AuthenticatedUsertype and@fastify/jwt/fastifymodule augmentations (typedrequest.userandapp.authenticate) - Updated route code to use strongly typed
request.user.idand safer optional logging - Updated
authenticatedecorator implementation to useFastifyRequest/FastifyReplytypes and a typedjwtVerifycall
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| apps/backend/src/types/fastify.d.ts | Adds JWT/Fastify TypeScript module augmentation and AuthenticatedUser type |
| apps/backend/src/routes/auth.ts | Removes any casts by using typed request.user |
| apps/backend/src/app.ts | Types authenticate decorator params and uses typed jwtVerify payload |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import { extractRawJwt, blocklistKey } from './utils/jwt.js'; | ||
| import { validateEnv } from './utils/validateEnv.js'; | ||
|
|
||
| import type { AuthenticatedUser } from './types/fastify.js'; |
There was a problem hiding this comment.
it's an import type, so it should probably get stripped out at compile time and nothing actually tries to load that file at runtime.
and for typechecking, the backend is set to moduleResolution: bundler, which means a .js import just maps to the matching .ts or .d.ts file, so ./types/fastify.js points at fastify.d.ts fine
anddd the typecheck passing in CI backs that up.
|
hmm i think it might be a false positive/ |
Harxhit
left a comment
There was a problem hiding this comment.
Merging this PR is required before contributors can start working on the linked sub-issues, as it provides the necessary foundation for those tasks. Approving
|
Congratulations @ramnnn2006 on getting PR #560 merged! Thank you for your contribution to the project. To receive the appropriate GSSoC labels and recognition, please mention @Harxhit in the #get-labels channel on our Discord server and share your merged PR link. |
summary
this PR removes the remaining
anyusages in the backend auth module and replaces them with proper fastify types and a dedicated jwt payload interface.this closes out the
auth.ts scope of the type-safety umbrella issue, sorequest.userand theauthenticate` decorator are now type-checked instead of being cast away.Fixes #541
type of Change
What Changed
9added an
AuthenticatedUser { id, username }interface inapps/backend/src/types/fastify.d.tsand augmented@fastify/jwt'sFastifyJWTinterface sorequest.useris typed. went throughFastiFastifyRequest.userdirectly since@fastify/jwt`already owns that field and doing it the other way would clashdeclared the
authenticatedecorator onFastifyInstan.authenticateis no longer untypedtyped the
authenticatedecorator inapps/backend/src/app.ts(request: any, reply: anytoFastifyRequest, FastifyReply) and passed theuser shape through `request.jwtVerify(
dropped the
(request.user as any)casts inapps/backend/src/routes/auth.ts(the/meroute and the secure logout route).How to Test
npm run typecheck -w apps/backendpasses with no errorsnpm run lint -w apps/backendstays cleannpx vitest run src/__tests__/logout.test.tsChecklist
npm run typecheck).console.logor debug statements left in the code.Screenshots / Recordings
n/a, no ui changes.
Additional Context