Skip to content

Fix duplicate CoreBetterAuthModule.forRoot() import causing DI corrup…#493

Closed
pascal-klesse wants to merge 1 commit intodevelopfrom
fix/duplicate-better-auth-module-import
Closed

Fix duplicate CoreBetterAuthModule.forRoot() import causing DI corrup…#493
pascal-klesse wants to merge 1 commit intodevelopfrom
fix/duplicate-better-auth-module-import

Conversation

@pascal-klesse
Copy link
Member

…tion

  • Add protection against multiple forRoot() calls in CoreBetterAuthModule

    • Cache the dynamic module and return it on subsequent calls
    • Log warning to help developers identify the misconfiguration
    • Skip cache in test environments (VITEST) to allow different test configs
    • Reset cache in reset() method for proper test isolation
  • Fix bug in CoreAuthModule.forRoot() where providers were incorrectly replaced with imports when options.providers was provided

    • Changed imports.concat(options.providers) to providers.concat(...)

This fixes the "Cannot read properties of undefined (reading 'getAll')" error that occurs when both CoreModule.forRoot(envConfig) and a separate IamModule import CoreBetterAuthModule.forRoot(), causing this.reflector to be undefined in RolesGuard.

…tion

- Add protection against multiple forRoot() calls in CoreBetterAuthModule
  - Cache the dynamic module and return it on subsequent calls
  - Log warning to help developers identify the misconfiguration
  - Skip cache in test environments (VITEST) to allow different test configs
  - Reset cache in reset() method for proper test isolation

- Fix bug in CoreAuthModule.forRoot() where providers were incorrectly
  replaced with imports when options.providers was provided
  - Changed `imports.concat(options.providers)` to `providers.concat(...)`

This fixes the "Cannot read properties of undefined (reading 'getAll')"
error that occurs when both CoreModule.forRoot(envConfig) and a separate
IamModule import CoreBetterAuthModule.forRoot(), causing this.reflector
to be undefined in RolesGuard.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@pascal-klesse pascal-klesse requested a review from Copilot February 5, 2026 14:46
Copy link
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

This PR fixes a critical dependency injection (DI) corruption issue that occurred when CoreBetterAuthModule.forRoot() was called multiple times, causing the RolesGuard's reflector to become undefined. The fix implements a caching mechanism for the dynamic module to prevent duplicate registrations and corrects a bug in CoreAuthModule where providers were incorrectly merged with imports.

Changes:

  • Added duplicate import protection in CoreBetterAuthModule.forRoot() with module caching
  • Fixed provider concatenation bug in CoreAuthModule.forRoot()
  • Added cache reset functionality for proper test isolation

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/core/modules/better-auth/core-better-auth.module.ts Implements caching mechanism to prevent multiple forRoot() calls from corrupting DI, adds warning messages, and includes formatting improvements
src/core/modules/auth/core-auth.module.ts Fixes bug where providers array was incorrectly replaced with imports array when custom providers were provided

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.

1 participant