Fix duplicate CoreBetterAuthModule.forRoot() import causing DI corrup…#493
Closed
pascal-klesse wants to merge 1 commit intodevelopfrom
Closed
Fix duplicate CoreBetterAuthModule.forRoot() import causing DI corrup…#493pascal-klesse wants to merge 1 commit intodevelopfrom
pascal-klesse wants to merge 1 commit intodevelopfrom
Conversation
…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>
Contributor
There was a problem hiding this comment.
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 |
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.
…tion
Add protection against multiple forRoot() calls in CoreBetterAuthModule
Fix bug in CoreAuthModule.forRoot() where providers were incorrectly replaced with imports when options.providers was provided
imports.concat(options.providers)toproviders.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.