@lenne.tech/nest-server 11.15.1 → 11.15.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/dist/core/common/interfaces/server-options.interface.d.ts +5 -0
  2. package/dist/core/modules/better-auth/better-auth.config.d.ts +17 -1
  3. package/dist/core/modules/better-auth/better-auth.config.js +72 -2
  4. package/dist/core/modules/better-auth/better-auth.config.js.map +1 -1
  5. package/dist/core/modules/better-auth/core-better-auth-api.middleware.js +1 -0
  6. package/dist/core/modules/better-auth/core-better-auth-api.middleware.js.map +1 -1
  7. package/dist/core/modules/better-auth/core-better-auth-cookie.helper.d.ts +3 -0
  8. package/dist/core/modules/better-auth/core-better-auth-cookie.helper.js +6 -1
  9. package/dist/core/modules/better-auth/core-better-auth-cookie.helper.js.map +1 -1
  10. package/dist/core/modules/better-auth/core-better-auth.controller.js +1 -0
  11. package/dist/core/modules/better-auth/core-better-auth.controller.js.map +1 -1
  12. package/dist/core/modules/better-auth/core-better-auth.module.d.ts +2 -0
  13. package/dist/core/modules/better-auth/core-better-auth.module.js +29 -9
  14. package/dist/core/modules/better-auth/core-better-auth.module.js.map +1 -1
  15. package/dist/core/modules/better-auth/core-better-auth.service.d.ts +4 -1
  16. package/dist/core/modules/better-auth/core-better-auth.service.js +11 -3
  17. package/dist/core/modules/better-auth/core-better-auth.service.js.map +1 -1
  18. package/dist/tsconfig.build.tsbuildinfo +1 -1
  19. package/package.json +1 -1
  20. package/src/core/common/interfaces/server-options.interface.ts +121 -0
  21. package/src/core/modules/better-auth/README.md +79 -17
  22. package/src/core/modules/better-auth/better-auth.config.ts +148 -4
  23. package/src/core/modules/better-auth/core-better-auth-api.middleware.ts +1 -0
  24. package/src/core/modules/better-auth/core-better-auth-cookie.helper.ts +18 -2
  25. package/src/core/modules/better-auth/core-better-auth.controller.ts +2 -1
  26. package/src/core/modules/better-auth/core-better-auth.module.ts +40 -9
  27. package/src/core/modules/better-auth/core-better-auth.service.ts +21 -0
@@ -19,7 +19,7 @@ import { ConfigService } from '../../common/services/config.service';
19
19
  import { RolesGuardRegistry } from '../auth/guards/roles-guard-registry';
20
20
  import { BetterAuthRolesGuard } from './better-auth-roles.guard';
21
21
  import { BetterAuthTokenService } from './better-auth-token.service';
22
- import { BetterAuthInstance, createBetterAuthInstance } from './better-auth.config';
22
+ import { BetterAuthInstance, CreateBetterAuthResult, createBetterAuthInstance } from './better-auth.config';
23
23
  import { DefaultBetterAuthResolver } from './better-auth.resolver';
24
24
  import { CoreBetterAuthApiMiddleware } from './core-better-auth-api.middleware';
25
25
  import { CoreBetterAuthChallengeService } from './core-better-auth-challenge.service';
@@ -31,7 +31,7 @@ import { CoreBetterAuthUserMapper } from './core-better-auth-user.mapper';
31
31
  import { CoreBetterAuthController } from './core-better-auth.controller';
32
32
  import { CoreBetterAuthMiddleware } from './core-better-auth.middleware';
33
33
  import { CoreBetterAuthResolver } from './core-better-auth.resolver';
34
- import { BETTER_AUTH_CONFIG, CoreBetterAuthService } from './core-better-auth.service';
34
+ import { BETTER_AUTH_CONFIG, BETTER_AUTH_COOKIE_DOMAIN, CoreBetterAuthService } from './core-better-auth.service';
35
35
 
36
36
  /**
37
37
  * Token for injecting the better-auth instance
@@ -235,6 +235,7 @@ export class CoreBetterAuthModule implements NestModule, OnModuleInit {
235
235
  private static serviceInstance: CoreBetterAuthService | null = null;
236
236
  private static userMapperInstance: CoreBetterAuthUserMapper | null = null;
237
237
  private static tokenServiceInstance: BetterAuthTokenService | null = null;
238
+ private static resolvedCookieDomain: string | undefined = undefined;
238
239
 
239
240
  /**
240
241
  * Gets the controller class to use (custom or default)
@@ -277,6 +278,14 @@ export class CoreBetterAuthModule implements NestModule, OnModuleInit {
277
278
  return this.tokenServiceInstance;
278
279
  }
279
280
 
281
+ /**
282
+ * Gets the resolved cookie domain for cross-subdomain cookie sharing.
283
+ * Returns the domain resolved during Better-Auth instance creation, or undefined if disabled.
284
+ */
285
+ static getCookieDomain(): string | undefined {
286
+ return this.resolvedCookieDomain;
287
+ }
288
+
280
289
  constructor(
281
290
  @Optional() private readonly betterAuthService?: CoreBetterAuthService,
282
291
  @Optional() private readonly rateLimiter?: CoreBetterAuthRateLimiter,
@@ -614,13 +623,15 @@ export class CoreBetterAuthModule implements NestModule, OnModuleInit {
614
623
 
615
624
  // Note: Secret validation is now handled in createBetterAuthInstance
616
625
  // with fallback to jwt.secret, jwt.refresh.secret, or auto-generation
617
- this.authInstance = createBetterAuthInstance({
626
+ const result = createBetterAuthInstance({
618
627
  config,
619
628
  db,
620
629
  fallbackSecrets,
621
630
  onEmailVerified,
622
631
  sendVerificationEmail,
623
632
  });
633
+ this.authInstance = result?.instance ?? null;
634
+ this.resolvedCookieDomain = result?.cookieDomain;
624
635
 
625
636
  // Store a config copy with the resolved secret so that consumers
626
637
  // (CoreBetterAuthService, CoreBetterAuthController) can sign cookies.
@@ -645,17 +656,25 @@ export class CoreBetterAuthModule implements NestModule, OnModuleInit {
645
656
  provide: BETTER_AUTH_CONFIG,
646
657
  useFactory: () => this.currentConfig,
647
658
  },
659
+ // Provide the resolved cookie domain for cross-subdomain cookie sharing
660
+ // IMPORTANT: Must depend on BETTER_AUTH_INSTANCE to ensure resolvedCookieDomain is set
661
+ {
662
+ inject: [BETTER_AUTH_INSTANCE],
663
+ provide: BETTER_AUTH_COOKIE_DOMAIN,
664
+ useFactory: () => this.resolvedCookieDomain,
665
+ },
648
666
  // CoreBetterAuthService needs to be a factory that explicitly depends on BETTER_AUTH_INSTANCE
649
667
  // to ensure proper initialization order
650
668
  {
651
- inject: [BETTER_AUTH_INSTANCE, BETTER_AUTH_CONFIG, getConnectionToken()],
669
+ inject: [BETTER_AUTH_INSTANCE, BETTER_AUTH_CONFIG, getConnectionToken(), BETTER_AUTH_COOKIE_DOMAIN],
652
670
  provide: CoreBetterAuthService,
653
671
  useFactory: (
654
672
  authInstance: BetterAuthInstance | null,
655
673
  resolvedConfig: IBetterAuth | null,
656
674
  connection: Connection,
675
+ cookieDomain: string | undefined,
657
676
  ) => {
658
- return new CoreBetterAuthService(authInstance, connection, resolvedConfig);
677
+ return new CoreBetterAuthService(authInstance, connection, resolvedConfig, undefined, cookieDomain);
659
678
  },
660
679
  },
661
680
  CoreBetterAuthUserMapper,
@@ -714,6 +733,7 @@ export class CoreBetterAuthModule implements NestModule, OnModuleInit {
714
733
  // Lazy GraphQL driver: Reset service references
715
734
  this.serviceInstance = null;
716
735
  this.userMapperInstance = null;
736
+ this.resolvedCookieDomain = undefined;
717
737
  // Reset shared RolesGuard registry (shared with CoreAuthModule)
718
738
  RolesGuardRegistry.reset();
719
739
  }
@@ -856,6 +876,7 @@ export class CoreBetterAuthModule implements NestModule, OnModuleInit {
856
876
  };
857
877
 
858
878
  // Connection is now guaranteed to be established
879
+ let result: CreateBetterAuthResult | null;
859
880
  const db = connection.db;
860
881
  if (!db) {
861
882
  // Fallback to global mongoose if connection.db is not yet available
@@ -864,16 +885,18 @@ export class CoreBetterAuthModule implements NestModule, OnModuleInit {
864
885
  if (!globalDb) {
865
886
  throw new Error('MongoDB database not available');
866
887
  }
867
- this.authInstance = createBetterAuthInstance({
888
+ result = createBetterAuthInstance({
868
889
  ...sharedInstanceOptions,
869
890
  db: globalDb,
870
891
  });
871
892
  } else {
872
- this.authInstance = createBetterAuthInstance({
893
+ result = createBetterAuthInstance({
873
894
  ...sharedInstanceOptions,
874
895
  db,
875
896
  });
876
897
  }
898
+ this.authInstance = result?.instance ?? null;
899
+ this.resolvedCookieDomain = result?.cookieDomain;
877
900
 
878
901
  // Store a config copy with the resolved secret (same as first forRoot variant)
879
902
  const fallbacks = options?.fallbackSecrets;
@@ -902,17 +925,25 @@ export class CoreBetterAuthModule implements NestModule, OnModuleInit {
902
925
  provide: BETTER_AUTH_CONFIG,
903
926
  useFactory: () => this.currentConfig,
904
927
  },
928
+ // Provide the resolved cookie domain for cross-subdomain cookie sharing
929
+ // IMPORTANT: Must depend on BETTER_AUTH_INSTANCE to ensure resolvedCookieDomain is set
930
+ {
931
+ inject: [BETTER_AUTH_INSTANCE],
932
+ provide: BETTER_AUTH_COOKIE_DOMAIN,
933
+ useFactory: () => this.resolvedCookieDomain,
934
+ },
905
935
  // CoreBetterAuthService needs to be a factory that explicitly depends on BETTER_AUTH_INSTANCE
906
936
  // to ensure proper initialization order
907
937
  {
908
- inject: [BETTER_AUTH_INSTANCE, BETTER_AUTH_CONFIG, getConnectionToken()],
938
+ inject: [BETTER_AUTH_INSTANCE, BETTER_AUTH_CONFIG, getConnectionToken(), BETTER_AUTH_COOKIE_DOMAIN],
909
939
  provide: CoreBetterAuthService,
910
940
  useFactory: (
911
941
  authInstance: BetterAuthInstance | null,
912
942
  resolvedConfig: IBetterAuth | null,
913
943
  connection: Connection,
944
+ cookieDomain: string | undefined,
914
945
  ) => {
915
- return new CoreBetterAuthService(authInstance, connection, resolvedConfig);
946
+ return new CoreBetterAuthService(authInstance, connection, resolvedConfig, undefined, cookieDomain);
916
947
  },
917
948
  },
918
949
  CoreBetterAuthUserMapper,
@@ -54,6 +54,12 @@ export interface SessionResult {
54
54
  */
55
55
  export const BETTER_AUTH_CONFIG = 'BETTER_AUTH_CONFIG';
56
56
 
57
+ /**
58
+ * Injection token for resolved cross-subdomain cookie domain.
59
+ * Set during Better-Auth instance creation, undefined if disabled.
60
+ */
61
+ export const BETTER_AUTH_COOKIE_DOMAIN = 'BETTER_AUTH_COOKIE_DOMAIN';
62
+
57
63
  @Injectable()
58
64
  export class CoreBetterAuthService {
59
65
  private readonly logger = new Logger(CoreBetterAuthService.name);
@@ -65,6 +71,7 @@ export class CoreBetterAuthService {
65
71
  @Inject(BETTER_AUTH_CONFIG) @Optional() private readonly resolvedConfig?: IBetterAuth | null,
66
72
  // ConfigService is last because it's only needed as fallback when resolvedConfig is not provided
67
73
  @Optional() private readonly configService?: ConfigService,
74
+ @Inject(BETTER_AUTH_COOKIE_DOMAIN) @Optional() private readonly cookieDomain?: string | null,
68
75
  ) {
69
76
  // Use resolvedConfig if provided (has fallback secret applied), otherwise get fresh from ConfigService
70
77
  // Better-Auth is enabled by default (zero-config) - only disabled if explicitly set to false
@@ -220,6 +227,20 @@ export class CoreBetterAuthService {
220
227
  return this.config.baseUrl || 'http://localhost:3000';
221
228
  }
222
229
 
230
+ /**
231
+ * Gets the resolved cookie domain for cross-subdomain cookie sharing.
232
+ *
233
+ * Returns the domain that was resolved during Better-Auth instance creation.
234
+ * The resolution follows the Boolean Shorthand Pattern and is performed once
235
+ * by `createBetterAuthInstance()` in `better-auth.config.ts`.
236
+ *
237
+ * @returns The cookie domain string, or undefined if cross-subdomain cookies are disabled
238
+ * @since 11.15.1
239
+ */
240
+ getCookieDomain(): string | undefined {
241
+ return this.cookieDomain ?? undefined;
242
+ }
243
+
223
244
  /**
224
245
  * Gets the session cookie name based on the configured base path.
225
246
  *