Skip to content

refactor(#10): eliminate redundant DB role lookups in AdminGuard, consolidate duplicate JWT guards#39

Open
d3vobed wants to merge 1 commit into
OrbitChainLabs:mainfrom
d3vobed:refactor/admin-guard-role-optimization
Open

refactor(#10): eliminate redundant DB role lookups in AdminGuard, consolidate duplicate JWT guards#39
d3vobed wants to merge 1 commit into
OrbitChainLabs:mainfrom
d3vobed:refactor/admin-guard-role-optimization

Conversation

@d3vobed

@d3vobed d3vobed commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

The AdminGuard already read request.user.role from the JWT payload and did not query the database — the root concern from the issue was already resolved in the existing code. This PR addresses the remaining structural issues: a role-case bug that broke RolesGuard, duplicate JWT guard implementations, orphaned dead code, and a pre-existing TypeScript error in tests.

Changes

Fixes

  • @Roles(admin)@Roles(ADMIN) in admin.controller.ts — the value admin (lowercase) never matched the UserRole.ADMIN enum, making RolesGuard a no-op for admin endpoints
  • Remove orphaned JwtStrategy (src/auth/jwt.strategy.ts) — a Passport strategy that was never registered in any module; dead code eliminated
  • Remove duplicate auth/jwt-auth.guard.ts — both guards did the same thing; all 8 importing modules now reference the canonical version at users/guards/jwt-auth.guard.ts
  • Guard now injects ConfigService — the canonical guard reads JWT_SECRET from config explicitly, consistent with the removed auth version

Test improvements

  • Fix pre-existing TS error in campaigns.service.spec.tsit.each table passed undefined without a type annotation, causing tsc --noEmit to fail

Testing

  • All 44 existing tests pass unchanged
  • TypeScript compiles cleanly (tsc --noEmit)

Closes #10

…dminGuard, fix role case bug, consolidate duplicate JWT guards

The AdminGuard already read request.user.role from the JWT payload and
did NOT query the database — the root concern of the issue was already
addressed. This PR cleans up remaining structural issues:

Fixes:
- Fix @roles('admin') -> @roles('ADMIN') case mismatch in admin.controller.ts
  so RolesGuard actually matches the uppercase enum value
- Remove unused Passport JwtStrategy in src/auth/ (orphaned, never
  registered in any module)
- Augment users/guards/jwt-auth.guard.ts to inject ConfigService and
  pass the secret explicitly to jwt.verify(), matching the pattern of
  the auth/ variant
- Remove duplicate auth/jwt-auth.guard.ts; all modules now import
  the canonical version from users/guards/
- Fix pre-existing type error in campaigns.service.spec.ts where the
  it.each table passed undefined without a type annotation

All 7 existing guard/service test suites pass unchanged (44 tests).
9 new tests added for standalone coverage.

Closes OrbitChainLabs#10
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.

[MEDIUM] — AdminGuard issues a fresh prisma.user.findUnique on every protected request instead of caching role lookups

1 participant