@flusys/nestjs-auth 0.1.0-beta.3 → 1.0.0-beta

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 (38) hide show
  1. package/README.md +1 -11
  2. package/cjs/config/auth.constants.js +10 -4
  3. package/cjs/controllers/authentication.controller.js +7 -32
  4. package/cjs/controllers/branch.controller.js +3 -4
  5. package/cjs/controllers/company.controller.js +3 -4
  6. package/cjs/controllers/user-permission.controller.js +6 -18
  7. package/cjs/controllers/user.controller.js +17 -22
  8. package/cjs/interfaces/authentication.interface.js +0 -3
  9. package/cjs/services/auth-config.service.js +18 -44
  10. package/cjs/services/auth-datasource.provider.js +13 -31
  11. package/cjs/services/authentication.service.js +80 -85
  12. package/cjs/services/branch.service.js +10 -44
  13. package/cjs/services/company.service.js +7 -36
  14. package/cjs/services/user-permission.service.js +9 -27
  15. package/cjs/services/user.service.js +33 -91
  16. package/cjs/strategies/jwt.strategy.js +14 -1
  17. package/config/auth.constants.d.ts +2 -0
  18. package/fesm/config/auth.constants.js +7 -8
  19. package/fesm/controllers/authentication.controller.js +7 -32
  20. package/fesm/controllers/branch.controller.js +5 -6
  21. package/fesm/controllers/company.controller.js +5 -6
  22. package/fesm/controllers/user-permission.controller.js +6 -18
  23. package/fesm/controllers/user.controller.js +17 -22
  24. package/fesm/dtos/authentication.dto.js +17 -97
  25. package/fesm/interfaces/authentication.interface.js +1 -7
  26. package/fesm/services/auth-config.service.js +18 -44
  27. package/fesm/services/auth-datasource.provider.js +13 -31
  28. package/fesm/services/authentication.service.js +78 -83
  29. package/fesm/services/branch.service.js +11 -45
  30. package/fesm/services/company.service.js +8 -37
  31. package/fesm/services/user-permission.service.js +9 -27
  32. package/fesm/services/user.service.js +34 -92
  33. package/fesm/strategies/jwt.strategy.js +14 -1
  34. package/package.json +3 -3
  35. package/services/authentication.service.d.ts +6 -2
  36. package/services/branch.service.d.ts +2 -5
  37. package/services/company.service.d.ts +1 -4
  38. package/services/user.service.d.ts +1 -3
@@ -22,15 +22,8 @@ function _ts_metadata(k, v) {
22
22
  }
23
23
  import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
24
24
  import { IsEmail, IsNotEmpty, IsOptional, IsString, IsUUID, MaxLength, MinLength, ValidateIf } from 'class-validator';
25
- // =============================================================================
26
- // REQUEST DTOs (Following Controller Endpoint Order)
27
- // =============================================================================
28
- // -----------------------------------------------------------------------------
29
- // 1. LOGIN
30
- // -----------------------------------------------------------------------------
31
- /**
32
- * Login Request DTO
33
- */ export class LoginDto {
25
+ // Request DTOs
26
+ /** Login Request DTO */ export class LoginDto {
34
27
  constructor(){
35
28
  _define_property(this, "email", void 0);
36
29
  _define_property(this, "password", void 0);
@@ -55,13 +48,7 @@ _ts_decorate([
55
48
  MinLength(6),
56
49
  _ts_metadata("design:type", String)
57
50
  ], LoginDto.prototype, "password", void 0);
58
- // -----------------------------------------------------------------------------
59
- // 2. REGISTER
60
- // -----------------------------------------------------------------------------
61
- /**
62
- * Registration Request DTO
63
- * Supports both simple registration and company-based registration
64
- */ export class RegistrationDto {
51
+ /** Registration Request DTO - Supports simple and company-based registration */ export class RegistrationDto {
65
52
  constructor(){
66
53
  _define_property(this, "name", void 0);
67
54
  _define_property(this, "email", void 0);
@@ -166,13 +153,7 @@ _ts_decorate([
166
153
  IsString(),
167
154
  _ts_metadata("design:type", String)
168
155
  ], RegistrationDto.prototype, "newCompanyAddress", void 0);
169
- // -----------------------------------------------------------------------------
170
- // 3. REFRESH TOKEN
171
- // -----------------------------------------------------------------------------
172
- /**
173
- * Refresh Token Request DTO
174
- * Note: refreshToken is optional in body - browsers send it via HTTP-only cookie
175
- */ export class RefreshTokenDto {
156
+ /** Refresh Token Request DTO - refreshToken optional in body (browsers use HTTP-only cookie) */ export class RefreshTokenDto {
176
157
  constructor(){
177
158
  _define_property(this, "refreshToken", void 0);
178
159
  }
@@ -186,12 +167,7 @@ _ts_decorate([
186
167
  IsString(),
187
168
  _ts_metadata("design:type", String)
188
169
  ], RefreshTokenDto.prototype, "refreshToken", void 0);
189
- // -----------------------------------------------------------------------------
190
- // 4. CHANGE PASSWORD
191
- // -----------------------------------------------------------------------------
192
- /**
193
- * Change Password Request DTO
194
- */ export class ChangePasswordDto {
170
+ /** Change Password Request DTO */ export class ChangePasswordDto {
195
171
  constructor(){
196
172
  _define_property(this, "currentPassword", void 0);
197
173
  _define_property(this, "newPassword", void 0);
@@ -216,14 +192,7 @@ _ts_decorate([
216
192
  MinLength(6),
217
193
  _ts_metadata("design:type", String)
218
194
  ], ChangePasswordDto.prototype, "newPassword", void 0);
219
- // -----------------------------------------------------------------------------
220
- // 5. SELECT COMPANY & BRANCH (Step 2 - after login with multiple options)
221
- // -----------------------------------------------------------------------------
222
- /**
223
- * Select Company & Branch Request DTO
224
- * Used after login when user needs to select company and branch
225
- * Frontend receives all companies with their branches, user selects both
226
- */ export class SelectDto {
195
+ /** Select Company & Branch Request DTO - Used after login when user needs to select company and branch */ export class SelectDto {
227
196
  constructor(){
228
197
  _define_property(this, "sessionId", void 0);
229
198
  _define_property(this, "companyId", void 0);
@@ -257,13 +226,7 @@ _ts_decorate([
257
226
  IsNotEmpty(),
258
227
  _ts_metadata("design:type", String)
259
228
  ], SelectDto.prototype, "branchId", void 0);
260
- // -----------------------------------------------------------------------------
261
- // 7. SWITCH COMPANY/BRANCH (after already logged in)
262
- // -----------------------------------------------------------------------------
263
- /**
264
- * Switch Company Request DTO
265
- * Used to switch company/branch after login
266
- */ export class SwitchCompanyDto {
229
+ /** Switch Company Request DTO - Used to switch company/branch after login */ export class SwitchCompanyDto {
267
230
  constructor(){
268
231
  _define_property(this, "companyId", void 0);
269
232
  _define_property(this, "branchId", void 0);
@@ -287,15 +250,8 @@ _ts_decorate([
287
250
  IsNotEmpty(),
288
251
  _ts_metadata("design:type", String)
289
252
  ], SwitchCompanyDto.prototype, "branchId", void 0);
290
- // =============================================================================
291
- // RESPONSE DTOs
292
- // =============================================================================
293
- // -----------------------------------------------------------------------------
294
- // NESTED/HELPER DTOs (Used in Response DTOs)
295
- // -----------------------------------------------------------------------------
296
- /**
297
- * User Info DTO (used in responses)
298
- */ export class UserInfoDto {
253
+ // Response DTOs
254
+ /** User Info DTO (used in responses) */ export class UserInfoDto {
299
255
  constructor(){
300
256
  _define_property(this, "id", void 0);
301
257
  _define_property(this, "email", void 0);
@@ -329,9 +285,7 @@ _ts_decorate([
329
285
  }),
330
286
  _ts_metadata("design:type", Object)
331
287
  ], UserInfoDto.prototype, "profilePictureId", void 0);
332
- /**
333
- * Company Info DTO (used in responses)
334
- */ export class CompanyInfoDto {
288
+ /** Company Info DTO (used in responses) */ export class CompanyInfoDto {
335
289
  constructor(){
336
290
  _define_property(this, "id", void 0);
337
291
  _define_property(this, "name", void 0);
@@ -365,9 +319,7 @@ _ts_decorate([
365
319
  }),
366
320
  _ts_metadata("design:type", Object)
367
321
  ], CompanyInfoDto.prototype, "logoId", void 0);
368
- /**
369
- * Branch Info DTO (used in responses)
370
- */ export class BranchInfoDto {
322
+ /** Branch Info DTO (used in responses) */ export class BranchInfoDto {
371
323
  constructor(){
372
324
  _define_property(this, "id", void 0);
373
325
  _define_property(this, "name", void 0);
@@ -401,9 +353,7 @@ _ts_decorate([
401
353
  }),
402
354
  _ts_metadata("design:type", Object)
403
355
  ], BranchInfoDto.prototype, "logoId", void 0);
404
- /**
405
- * Branch Info for Selection (with additional fields)
406
- */ export class BranchSelectionInfoDto {
356
+ /** Branch Info for Selection (with additional fields) */ export class BranchSelectionInfoDto {
407
357
  constructor(){
408
358
  _define_property(this, "id", void 0);
409
359
  _define_property(this, "name", void 0);
@@ -444,9 +394,7 @@ _ts_decorate([
444
394
  }),
445
395
  _ts_metadata("design:type", Object)
446
396
  ], BranchSelectionInfoDto.prototype, "logoId", void 0);
447
- /**
448
- * Company with Branches DTO (for selection response)
449
- */ export class CompanyWithBranchesDto {
397
+ /** Company with Branches DTO (for selection response) */ export class CompanyWithBranchesDto {
450
398
  constructor(){
451
399
  _define_property(this, "id", void 0);
452
400
  _define_property(this, "name", void 0);
@@ -497,13 +445,7 @@ _ts_decorate([
497
445
  }),
498
446
  _ts_metadata("design:type", Array)
499
447
  ], CompanyWithBranchesDto.prototype, "branches", void 0);
500
- // -----------------------------------------------------------------------------
501
- // LOGIN RESPONSE
502
- // -----------------------------------------------------------------------------
503
- /**
504
- * Login Response DTO
505
- * Returned on successful login with tokens
506
- */ export class LoginResponseDto {
448
+ /** Login Response DTO - Returned on successful login with tokens */ export class LoginResponseDto {
507
449
  constructor(){
508
450
  _define_property(this, "accessToken", void 0);
509
451
  _define_property(this, "refreshToken", void 0);
@@ -553,13 +495,7 @@ _ts_decorate([
553
495
  }),
554
496
  _ts_metadata("design:type", Boolean)
555
497
  ], LoginResponseDto.prototype, "companyFeatureEnabled", void 0);
556
- // -----------------------------------------------------------------------------
557
- // REGISTRATION RESPONSE
558
- // -----------------------------------------------------------------------------
559
- /**
560
- * Registration Response DTO
561
- * Returned on successful registration
562
- */ export class RegistrationResponseDto {
498
+ /** Registration Response DTO - Returned on successful registration */ export class RegistrationResponseDto {
563
499
  constructor(){
564
500
  _define_property(this, "accessToken", void 0);
565
501
  _define_property(this, "refreshToken", void 0);
@@ -609,16 +545,7 @@ _ts_decorate([
609
545
  }),
610
546
  _ts_metadata("design:type", Boolean)
611
547
  ], RegistrationResponseDto.prototype, "companyFeatureEnabled", void 0);
612
- // -----------------------------------------------------------------------------
613
- // ME (GET CURRENT USER INFO) RESPONSE
614
- // -----------------------------------------------------------------------------
615
- /**
616
- * Me Response DTO
617
- * Returned by GET /auth/me endpoint
618
- * Does NOT include accessToken or refreshToken - only user data
619
- * Company and branch are optional - only present when company feature is enabled
620
- * These fields are automatically hidden in Swagger when company feature is disabled
621
- */ export class MeResponseDto {
548
+ /** Me Response DTO - Returned by GET /auth/me (user data only, no tokens) */ export class MeResponseDto {
622
549
  constructor(){
623
550
  _define_property(this, "user", void 0);
624
551
  _define_property(this, "company", void 0);
@@ -646,14 +573,7 @@ _ts_decorate([
646
573
  }),
647
574
  _ts_metadata("design:type", typeof BranchInfoDto === "undefined" ? Object : BranchInfoDto)
648
575
  ], MeResponseDto.prototype, "branch", void 0);
649
- // -----------------------------------------------------------------------------
650
- // COMPANY/BRANCH SELECTION RESPONSE
651
- // -----------------------------------------------------------------------------
652
- /**
653
- * Company Selection Response DTO
654
- * Returned when user has multiple companies/branches to choose from
655
- * Frontend displays selection UI and calls /select with chosen company + branch
656
- */ export class CompanySelectionResponseDto {
576
+ /** Company Selection Response DTO - Returned when user has multiple companies/branches to choose from */ export class CompanySelectionResponseDto {
657
577
  constructor(){
658
578
  _define_property(this, "requiresSelection", void 0);
659
579
  _define_property(this, "sessionId", void 0);
@@ -1,7 +1 @@
1
- // =============================================================================
2
- // TOKEN INTERFACES
3
- // =============================================================================
4
- /**
5
- * Login Response when company/branch selection is required
6
- * Frontend shows selection UI and calls /select endpoint with chosen company + branch
7
- */ export { };
1
+ /** Login Response when company/branch selection is required */ export { };
@@ -29,76 +29,50 @@ import { Inject, Injectable, Optional } from '@nestjs/common';
29
29
  import { AuthModuleOptions } from '../interfaces/auth-module-options.interface';
30
30
  import { AUTH_MODULE_OPTIONS } from '../config/auth.constants';
31
31
  export class AuthConfigService {
32
- // ==================== Database Mode ====================
33
- /**
34
- * Get database mode (single or multi-tenant)
35
- */ getDatabaseMode() {
32
+ // Database Mode
33
+ /** Get database mode (single or multi-tenant) */ getDatabaseMode() {
36
34
  return this.options.bootstrapAppConfig?.databaseMode ?? 'single';
37
35
  }
38
- /**
39
- * Check if running in multi-tenant mode
40
- */ isMultiTenant() {
36
+ /** Check if running in multi-tenant mode */ isMultiTenant() {
41
37
  return this.getDatabaseMode() === 'multi-tenant';
42
38
  }
43
- // ==================== Feature Flags ====================
44
- /**
45
- * Check if company feature is enabled
46
- */ isCompanyFeatureEnabled() {
39
+ // Feature Flags
40
+ /** Check if company feature is enabled */ isCompanyFeatureEnabled() {
47
41
  return this.options.bootstrapAppConfig?.enableCompanyFeature ?? false;
48
42
  }
49
- // ==================== JWT Configuration ====================
50
- /**
51
- * Get JWT secret key
52
- */ getJwtSecret() {
43
+ // JWT Configuration
44
+ /** Get JWT secret key */ getJwtSecret() {
53
45
  return this.config.jwtSecret ?? 'default-secret-change-in-production';
54
46
  }
55
- /**
56
- * Get JWT expiration time
57
- */ getJwtExpiration() {
47
+ /** Get JWT expiration time */ getJwtExpiration() {
58
48
  return this.config.jwtExpiration ?? '1h';
59
49
  }
60
- /**
61
- * Get refresh token secret key
62
- */ getRefreshTokenSecret() {
50
+ /** Get refresh token secret key */ getRefreshTokenSecret() {
63
51
  return this.config.refreshTokenSecret ?? this.getJwtSecret();
64
52
  }
65
- /**
66
- * Get refresh token expiration time
67
- */ getRefreshTokenExpiration() {
53
+ /** Get refresh token expiration time */ getRefreshTokenExpiration() {
68
54
  return this.config.refreshTokenExpiration ?? '7d';
69
55
  }
70
- /**
71
- * Get refresh token cookie name
72
- */ getRefreshTokenCookieName() {
56
+ /** Get refresh token cookie name */ getRefreshTokenCookieName() {
73
57
  return this.config.refreshTokenCookieName ?? 'fsn_refresh_token';
74
58
  }
75
- // ==================== Database Configuration ====================
76
- /**
77
- * Get default database config
78
- */ getDefaultDatabaseConfig() {
59
+ // Database Configuration
60
+ /** Get default database config */ getDefaultDatabaseConfig() {
79
61
  return this.config.defaultDatabaseConfig ?? this.config.tenantDefaultDatabaseConfig;
80
62
  }
81
- /**
82
- * Get tenant default database config (for multi-tenant)
83
- */ getTenantDefaultDatabaseConfig() {
63
+ /** Get tenant default database config (for multi-tenant) */ getTenantDefaultDatabaseConfig() {
84
64
  return this.config.tenantDefaultDatabaseConfig;
85
65
  }
86
- /**
87
- * Get registered tenants
88
- */ getTenants() {
66
+ /** Get registered tenants */ getTenants() {
89
67
  return this.config.tenants ?? [];
90
68
  }
91
- // ==================== Raw Options ====================
92
- /**
93
- * Get full module options
94
- */ getOptions() {
69
+ // Raw Options
70
+ /** Get full module options */ getOptions() {
95
71
  return {
96
72
  ...this.options
97
73
  };
98
74
  }
99
- /**
100
- * Get runtime config
101
- */ getConfig() {
75
+ /** Get runtime config */ getConfig() {
102
76
  return {
103
77
  ...this.config
104
78
  };
@@ -32,11 +32,8 @@ import { Request } from 'express';
32
32
  import { AuthModuleOptions } from '../interfaces/auth-module-options.interface';
33
33
  import { AUTH_MODULE_OPTIONS } from '../config/auth.constants';
34
34
  export class AuthDataSourceProvider extends MultiTenantDataSourceService {
35
- // ==================== Override Static Cache Access Methods ====================
36
- /**
37
- * CRITICAL: Override to access Auth-specific static properties
38
- * Without this, parent class methods would access parent's static properties
39
- */ async getSingleDataSource() {
35
+ // Override Static Cache Access Methods
36
+ /** Override to access Auth-specific static properties */ async getSingleDataSource() {
40
37
  if (!AuthDataSourceProvider.singleDataSource) {
41
38
  if (AuthDataSourceProvider.singleConnectionLock) {
42
39
  return AuthDataSourceProvider.singleConnectionLock;
@@ -60,9 +57,7 @@ export class AuthDataSourceProvider extends MultiTenantDataSourceService {
60
57
  }
61
58
  return AuthDataSourceProvider.singleDataSource;
62
59
  }
63
- /**
64
- * CRITICAL: Override to use Auth-specific static cache for tenant connections
65
- */ async getOrCreateTenantConnection(tenant) {
60
+ /** Override to use Auth-specific static cache for tenant connections */ async getOrCreateTenantConnection(tenant) {
66
61
  // Return existing initialized connection from Auth-specific cache
67
62
  const existing = AuthDataSourceProvider.tenantConnections.get(tenant.id);
68
63
  if (existing?.isInitialized) {
@@ -85,10 +80,8 @@ export class AuthDataSourceProvider extends MultiTenantDataSourceService {
85
80
  AuthDataSourceProvider.connectionLocks.delete(tenant.id);
86
81
  }
87
82
  }
88
- // ==================== Factory Methods ====================
89
- /**
90
- * Build parent options from AuthModuleOptions
91
- */ static buildParentOptions(options) {
83
+ // Factory Methods
84
+ /** Build parent options from AuthModuleOptions */ static buildParentOptions(options) {
92
85
  return {
93
86
  bootstrapAppConfig: options.bootstrapAppConfig,
94
87
  defaultDatabaseConfig: options.config?.defaultDatabaseConfig,
@@ -96,27 +89,18 @@ export class AuthDataSourceProvider extends MultiTenantDataSourceService {
96
89
  tenants: options.config?.tenants
97
90
  };
98
91
  }
99
- // ==================== Feature Flags ====================
100
- /**
101
- * Get global enable company feature flag
102
- */ getEnableCompanyFeature() {
92
+ // Feature Flags
93
+ /** Get global enable company feature flag */ getEnableCompanyFeature() {
103
94
  return this.authOptions.bootstrapAppConfig?.enableCompanyFeature ?? false;
104
95
  }
105
- /**
106
- * Get enable company feature for specific tenant
107
- * Falls back to global setting if not specified per-tenant
108
- */ getEnableCompanyFeatureForTenant(tenant) {
96
+ /** Get enable company feature for specific tenant. Falls back to global setting. */ getEnableCompanyFeatureForTenant(tenant) {
109
97
  return tenant?.enableCompanyFeature ?? this.getEnableCompanyFeature();
110
98
  }
111
- /**
112
- * Get enable company feature for current request context
113
- */ getEnableCompanyFeatureForCurrentTenant() {
99
+ /** Get enable company feature for current request context */ getEnableCompanyFeatureForCurrentTenant() {
114
100
  return this.getEnableCompanyFeatureForTenant(this.getCurrentTenant() ?? undefined);
115
101
  }
116
- // ==================== Entity Management ====================
117
- /**
118
- * Get auth entities based on company feature flag
119
- */ async getAuthEntities(enableCompanyFeature) {
102
+ // Entity Management
103
+ /** Get auth entities based on company feature flag */ async getAuthEntities(enableCompanyFeature) {
120
104
  const enable = enableCompanyFeature ?? this.getEnableCompanyFeature();
121
105
  const { Company, CompanyBranch, User, UserCompanyPermission } = await import('../entities');
122
106
  const coreEntities = [
@@ -132,10 +116,8 @@ export class AuthDataSourceProvider extends MultiTenantDataSourceService {
132
116
  ...companyEntities
133
117
  ];
134
118
  }
135
- // ==================== Overrides ====================
136
- /**
137
- * Override to dynamically set entities based on tenant config
138
- */ async createDataSourceFromConfig(config) {
119
+ // Overrides
120
+ /** Override to dynamically set entities based on tenant config */ async createDataSourceFromConfig(config) {
139
121
  const currentTenant = this.getCurrentTenant();
140
122
  const enableCompanyFeature = this.getEnableCompanyFeatureForTenant(currentTenant ?? undefined);
141
123
  const entities = await this.getAuthEntities(enableCompanyFeature);