Skip to content

Fix RolesGuard so a bare @Roles() allows access (#10) - #110

Merged
memplethee-lab merged 1 commit into
LintError:mainfrom
Fire162:fix/roles-guard-empty-roles
Oct 9, 2026
Merged

memplethee-lab merged 1 commit into
LintError:mainfrom
Fire162:fix/roles-guard-empty-roles

Conversation

@Fire162

@Fire162 Fire162 commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes the role check in RolesGuard.canActivate when routes are decorated with @Roles() without arguments.

Previously, calling @Roles() with no arguments set ROLES_KEY metadata to []. Because [] is truthy in JavaScript, !requiredRoles evaluated to false, causing RolesGuard to check for request['admin'] and run [].some(...), which returned false. This blocked unauthenticated access to admin auth endpoints (login, otp/generate, and otp/verify).

Changes

  • Updated RolesGuard.canActivate in src/modules/admin/guards/roles.guard.ts to check if (!requiredRoles || requiredRoles.length === 0) and return true.
  • Added an explicit ForbiddenException when an authenticated admin's role is not included in the required roles list.
  • Created src/modules/admin/guards/roles.guard.spec.ts with unit tests covering undefined metadata, empty metadata array, matching role, non-matching role, and missing request.admin.

Verification

  • Ran npx jest src/modules/admin/guards/roles.guard.spec.ts (6/6 tests passing).
  • Ran npm test (all 6 test suites, 26 tests passing).
  • Ran npm run lint (0 errors).
  • Ran npm run format.

Fixes #10

- 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
@memplethee-lab
memplethee-lab merged commit da8738c into LintError:main Oct 9, 2026
2 checks passed
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.

Fix RolesGuard so a bare @Roles() allows access (admin login is currently unreachable)

2 participants