Skip to content

feat(auth): implement JWT authentication middleware#404

Open
YaronZaki wants to merge 3 commits into
Dfunder:mainfrom
YaronZaki:fix/issue-365-jwt-auth-middleware
Open

feat(auth): implement JWT authentication middleware#404
YaronZaki wants to merge 3 commits into
Dfunder:mainfrom
YaronZaki:fix/issue-365-jwt-auth-middleware

Conversation

@YaronZaki

@YaronZaki YaronZaki commented Jun 29, 2026

Copy link
Copy Markdown

Implements a global JWT authentication guard for the StellarAid API.

  • Adds a project-wide JwtAuthGuard registered via APP_GUARD so all routes require a valid bearer token unless explicitly opted out via @Public().
  • JwtStrategy.validate now returns a typed JwtPayload (sub, email, role) instead of the full User document; consumers reading req.user via @CurrentUser() get a stable contract.
  • Moves the @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 prevented upstream/main from compiling cleanly.
  • Marks POST /auth/register as @Public() so unauthenticated users can register while every other route remains protected.

Closes #365
Closes #362
Closes #363
Closes #364

chore and others added 3 commits June 28, 2026 18:36
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).
@drips-wave

drips-wave Bot commented Jun 29, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement JWT Authentication Middleware Implement Login Endpoint with JWT Setup Nodemailer Email Service Introduce Email Verification system

2 participants