feat(auth): ship reset-password endpoint (#369)#405
Merged
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).
- PATCH /api/auth/reset-password/:token validates the token (SHA-256 digest + expiry), bcrypt-hashes the new password, and clears the reset token, reset expiry, refresh-token hash, and refresh-token expiry in a single update. - All four failure modes (missing/unknown/null-expiry/expired token) emit the same generic message so the endpoint leaks no info. - Endpoint is @public() and protected by AuthThrottlerGuard (10 req / 15 min / IP) layered on top of the global ThrottlerGuard.
|
@dzekojohn4 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! 🚀 |
4 tasks
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.
Closes #369
Closes #368
Closes #367
Closes #366
What
Implements
PATCH /api/auth/reset-password/:token. Validates the token (SHA-256 digest + expiry), bcrypt-hashes the new password, and clears the reset token plus all refresh-token state in a single update.Endpoint
200- password reset ({ success: true, message: "Password reset successfully. Please log in with your new password." })400- token missing, malformed, unknown, or expired (generic message - does not leak which condition failed)Security decisions
sha256(rawToken)rather than the raw token. A DB snapshot leak cannot be replayed against any pending reset.hashResetToken()is exported fromAuthServiceso the future forgot-password flow (Implement Forgot Password Endpoint #368) can mint and store tokens using the same digest lookup.usersService.update()usesfindOneAndUpdate, which bypasses the pre-save hook. The new password is hashed manually before being passed in.passwordResetToken,passwordResetExpires,refreshTokenHash, andrefreshTokenExpiresare all cleared so previously issued refresh tokens can no longer be used.AuthThrottlerGuard(10 req / 15 min / IP) layered on top of the globalThrottlerGuard.findOneAndUpdatepre-hooks exclude soft-deleted users, so a deactivated account cannot have its password reset.Invalid or expired reset tokenmessage - no info leak between "token doesn't exist" and "token expired".Files
src/auth/dto/reset-password.dto.ts- body validation (@IsString,@MinLength(8),@MaxLength(128))src/auth/auth.service.ts-resetPassword(token, newPassword)+ exportedhashResetToken(token)helpersrc/auth/auth.controller.ts-PATCH reset-password/:tokenwith@Public()+@UseGuards(AuthThrottlerGuard);BadRequestExceptionmapped to 400 envelopesrc/users/users.service.ts- docstring clarified onfindByPasswordResetToken(caller must pass hashed input)src/auth/auth.service.spec.ts- 4 new tests: happy path, unknown token, expired token, same-message invariantTests
Four unit tests cover the new
AuthService.resetPassword:findByPasswordResetTokencalled with the SHA-256 digest; update carries a bcrypt-hashed new password and clears all four token-bearing fields.BadRequestException;usersService.updatenever called.BadRequestException;usersService.updatenever called.Out of scope
POST /api/auth/forgot-passwordrequest endpoint (issue Implement Forgot Password Endpoint #368) that mints and emails reset tokens is a separate PR;hashResetTokenis exported so it can share the same digest lookup convention.