Skip to content

fix(server): inject ConfigService into ApiTokenGuard via useFactory#30

Merged
CoderCoco merged 1 commit into
mainfrom
claude/fix-make-dev-error-S1w28
Apr 29, 2026
Merged

fix(server): inject ConfigService into ApiTokenGuard via useFactory#30
CoderCoco merged 1 commit into
mainfrom
claude/fix-make-dev-error-S1w28

Conversation

@CoderCoco
Copy link
Copy Markdown
Owner

Summary

Updated the ApiTokenGuard provider configuration to use a factory function that injects the ConfigService dependency, allowing the guard to access configuration at runtime.

Key Changes

  • Added import of ConfigService to app.module.ts
  • Changed ApiTokenGuard provider from a simple class provider to a factory provider
  • The factory function receives ConfigService as an injected dependency and passes it to the ApiTokenGuard constructor
  • This enables ApiTokenGuard to access configuration values during authentication checks

Implementation Details

The provider configuration was updated from:

providers: [{ provide: APP_GUARD, useClass: ApiTokenGuard }]

To:

providers: [
  {
    provide: APP_GUARD,
    useFactory: (config: ConfigService) => new ApiTokenGuard(config),
    inject: [ConfigService],
  },
]

This change allows ApiTokenGuard to be instantiated with configuration dependencies, making it more flexible for token validation logic that may depend on runtime configuration.

https://claude.ai/code/session_01TutexsuMCT24musZH7MH72

tsx uses esbuild which does not emit TypeScript's emitDecoratorMetadata,
so Reflect.getMetadata('design:paramtypes', ApiTokenGuard) returns
undefined. Nest then instantiates the guard with no arguments, leaving
this.config undefined and crashing on every request.

Switching APP_GUARD from useClass to useFactory + inject makes the
dependency explicit and bypasses the metadata requirement entirely.

https://claude.ai/code/session_01TutexsuMCT24musZH7MH72
Copilot AI review requested due to automatic review settings April 29, 2026 02:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the Nest APP_GUARD registration for ApiTokenGuard to be constructed via a factory that injects ConfigService, enabling the guard to read runtime configuration.

Changes:

  • Import ConfigService in AppModule.
  • Replace the APP_GUARD useClass provider with a useFactory provider that injects ConfigService.

Comment on lines +55 to +60
providers: [
{
provide: APP_GUARD,
useFactory: (config: ConfigService) => new ApiTokenGuard(config),
inject: [ConfigService],
},
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR title doesn’t follow the Conventional Commits squash-merge format (<type>(<scope>): <imperative summary>) required by CONTRIBUTING.md. Please rename it to something like refactor(server): inject ConfigService into ApiTokenGuard (or another appropriate type/scope).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — PR title updated to fix(server): inject ConfigService into ApiTokenGuard via useFactory.


Generated by Claude Code

@CoderCoco CoderCoco changed the title Inject ConfigService into ApiTokenGuard via factory provider fix(server): inject ConfigService into ApiTokenGuard via useFactory Apr 29, 2026
@CoderCoco CoderCoco merged commit 16761cd into main Apr 29, 2026
10 checks passed
@CoderCoco CoderCoco deleted the claude/fix-make-dev-error-S1w28 branch April 29, 2026 02:38
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.

3 participants