feat(auth): implement JWT authentication middleware#404
Open
YaronZaki wants to merge 3 commits into
Open
Conversation
The recent upstream pull left two parallel copies of JWT strategy/guard code (root auth/* and auth/strategies|guards/*). Each pair contained broken syntax: auth.module.ts had a duplicate JwtStrategy import and src/auth/jwt-auth.guard.ts had two smushed class declarations. Consolidate to one canonical location: src/auth/jwt.strategy.ts, src/auth/jwt-auth.guard.ts, and add the missing src/auth/decorators/public.decorator.ts that the unified guard depends on. No behavior change.
## JWT auth middleware
- New global `JwtAuthGuard` registered via `APP_GUARD`; honours `@Public()` opt-out through `Reflector`.
- `JwtStrategy` returns a `JwtPayload` (`{sub, email, role}`) instead of the full user.
- New `@CurrentUser()` and `@Public()` decorators under `src/auth/decorators/`.
- `JwtPayload.role` narrowed from `string` to `UserRole`; `RolesGuard` and the admin controller consume the enum directly.
## Consumer migration
- `UsersController.getKycStatus` fetches the full user via `UsersService.findById(currentUser.sub)`.
- `UsersController.submitKyc` and `KycController.submitKyc` pass `currentUser.sub` (no more `req.user._id` reads).
- `HealthController` and `AuthController.register` marked `@Public()` so the global guard doesn't 401 them.
- New `JwtPayload` shape is the published contract; downstream modules depend on `currentUser.sub/email/role`.
## Duplicate KYC route consolidated
- Two parallel KYC stacks collided on `POST /users/me/kyc`. Kept the canonical `src/kyc/` stack (`KycController`, `KycService`, `KycSchema` — proper disk storage, UUID filenames, `UsersService` layering, admin review methods).
- Deleted `src/users/kyc.service.ts`, `src/users/schemas/kyc.schema.ts`, the duplicate `submitKyc` handler on `UsersController`, and the duplicate `Kyc` Mongo `forFeature` registration in `UsersModule` (which also resolved a duplicate `name: Kyc` Mongoose registration).
- Patched canonical `KycService.create()` to stamp `user.kycSubmissionDate` on submission and `KycService.updateStatus()` to stamp `user.kycReviewNotes` on review — both required for `GET /users/me/kyc` to return meaningful data.
## Housekeeping
- Moved `src/auth/jwt-auth.guard.ts` → `src/auth/guards/jwt-auth.guard.ts`.
- Deleted dead duplicate `src/decorators/public.decorator.ts`.
- Documented the `APP_GUARD` execution order in `src/app.module.ts` (ThrottlerGuard → JwtAuthGuard → `@Public()` short-circuit).
- Dropped redundant `JwtAuthModule` import in `UsersModule` (the global guard covers it).
|
@YaronZaki Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Implements a global JWT authentication guard for the StellarAid API.
JwtAuthGuardregistered viaAPP_GUARDso all routes require a valid bearer token unless explicitly opted out via@Public().JwtStrategy.validatenow returns a typedJwtPayload(sub,email,role) instead of the full User document; consumers readingreq.uservia@CurrentUser()get a stable contract.@Public()decorator to live next to the auth code (src/auth/decorators/public.decorator.ts) and consolidates the JWT guard/strategy files. This removes the duplicate guards and stale imports that preventedupstream/mainfrom compiling cleanly.POST /auth/registeras@Public()so unauthenticated users can register while every other route remains protected.Closes #365
Closes #362
Closes #363
Closes #364