Repository navigation
Fix RolesGuard so a bare @Roles() allows access (#10) - #110
Merged
memplethee-lab merged 1 commit intoOct 9, 2026
Merged
Conversation
- Handle empty requiredRoles array in RolesGuard.canActivate so routes decorated with bare @roles() allow access. - Throw ForbiddenException explicitly when an authenticated admin does not have a required role. - Add unit tests in roles.guard.spec.ts covering undefined metadata, empty metadata, matching role, non-matching role, and missing admin. Fixes LintError#10
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.
Summary
Fixes the role check in
RolesGuard.canActivatewhen routes are decorated with@Roles()without arguments.Previously, calling
@Roles()with no arguments setROLES_KEYmetadata to[]. Because[]is truthy in JavaScript,!requiredRolesevaluated tofalse, causingRolesGuardto check forrequest['admin']and run[].some(...), which returnedfalse. This blocked unauthenticated access to admin auth endpoints (login,otp/generate, andotp/verify).Changes
RolesGuard.canActivateinsrc/modules/admin/guards/roles.guard.tsto checkif (!requiredRoles || requiredRoles.length === 0)and returntrue.ForbiddenExceptionwhen an authenticated admin's role is not included in the required roles list.src/modules/admin/guards/roles.guard.spec.tswith unit tests covering undefined metadata, empty metadata array, matching role, non-matching role, and missingrequest.admin.Verification
npx jest src/modules/admin/guards/roles.guard.spec.ts(6/6 tests passing).npm test(all 6 test suites, 26 tests passing).npm run lint(0 errors).npm run format.Fixes #10