@flusys/nestjs-auth 1.1.0-beta → 2.0.0
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.
- package/README.md +590 -473
- package/cjs/config/auth.constants.js +8 -0
- package/cjs/controllers/authentication.controller.js +218 -39
- package/cjs/controllers/branch.controller.js +44 -1
- package/cjs/controllers/company-selection.controller.js +27 -20
- package/cjs/controllers/company.controller.js +44 -1
- package/cjs/controllers/user-permission.controller.js +82 -154
- package/cjs/controllers/user.controller.js +68 -15
- package/cjs/docs/auth-swagger.config.js +14 -7
- package/cjs/dtos/authentication.dto.js +16 -43
- package/cjs/dtos/index.js +0 -1
- package/cjs/dtos/user-permission.dto.js +28 -0
- package/cjs/dtos/user.dto.js +19 -4
- package/cjs/entities/user-company-permission.entity.js +0 -6
- package/cjs/interceptors/clear-token.interceptor.js +10 -23
- package/cjs/interceptors/set-token.interceptor.js +15 -55
- package/cjs/interfaces/auth-email-provider.interface.js +1 -1
- package/cjs/modules/auth.module.js +63 -147
- package/cjs/services/auth-config.service.js +22 -28
- package/cjs/services/auth-datasource.provider.js +9 -21
- package/cjs/services/auth-email.service.js +89 -85
- package/cjs/services/authentication.service.js +225 -396
- package/cjs/services/branch.service.js +9 -4
- package/cjs/services/company-selection-session.service.js +1 -1
- package/cjs/services/user-permission.service.js +43 -73
- package/cjs/services/user.service.js +78 -64
- package/cjs/strategies/jwt.strategy.js +44 -44
- package/cjs/validators/password.validator.js +45 -0
- package/config/auth.constants.d.ts +2 -0
- package/controllers/authentication.controller.d.ts +5 -3
- package/controllers/company-selection.controller.d.ts +6 -2
- package/controllers/user-permission.controller.d.ts +5 -4
- package/controllers/user.controller.d.ts +6 -0
- package/dtos/authentication.dto.d.ts +0 -4
- package/dtos/company-branch.dto.d.ts +0 -1
- package/dtos/company.dto.d.ts +0 -1
- package/dtos/index.d.ts +0 -1
- package/dtos/user-permission.dto.d.ts +4 -0
- package/dtos/user.dto.d.ts +1 -3
- package/entities/index.d.ts +3 -3
- package/entities/user-company-permission.entity.d.ts +0 -2
- package/fesm/config/auth.constants.js +6 -13
- package/fesm/controllers/authentication.controller.js +221 -42
- package/fesm/controllers/branch.controller.js +45 -2
- package/fesm/controllers/company-selection.controller.js +28 -21
- package/fesm/controllers/company.controller.js +45 -2
- package/fesm/controllers/user-permission.controller.js +84 -156
- package/fesm/controllers/user.controller.js +71 -18
- package/fesm/docs/auth-swagger.config.js +14 -7
- package/fesm/dtos/authentication.dto.js +17 -44
- package/fesm/dtos/index.js +0 -1
- package/fesm/dtos/user-permission.dto.js +28 -0
- package/fesm/dtos/user.dto.js +20 -5
- package/fesm/entities/user-company-permission.entity.js +0 -6
- package/fesm/interceptors/clear-token.interceptor.js +9 -22
- package/fesm/interceptors/set-token.interceptor.js +14 -54
- package/fesm/interfaces/auth-email-provider.interface.js +1 -1
- package/fesm/modules/auth.module.js +64 -148
- package/fesm/services/auth-config.service.js +23 -29
- package/fesm/services/auth-datasource.provider.js +10 -22
- package/fesm/services/auth-email.service.js +90 -86
- package/fesm/services/authentication.service.js +225 -396
- package/fesm/services/branch.service.js +9 -4
- package/fesm/services/company-selection-session.service.js +1 -1
- package/fesm/services/user-permission.service.js +44 -74
- package/fesm/services/user.service.js +79 -65
- package/fesm/strategies/jwt.strategy.js +44 -44
- package/fesm/validators/password.validator.js +48 -0
- package/interceptors/clear-token.interceptor.d.ts +0 -1
- package/interceptors/set-token.interceptor.d.ts +1 -1
- package/interfaces/authentication.interface.d.ts +0 -4
- package/interfaces/user-enricher.interface.d.ts +1 -4
- package/modules/auth.module.d.ts +6 -3
- package/package.json +4 -4
- package/services/auth-config.service.d.ts +6 -8
- package/services/auth-datasource.provider.d.ts +2 -4
- package/services/auth-email.service.d.ts +8 -4
- package/services/authentication.service.d.ts +15 -16
- package/services/company-selection-session.service.d.ts +1 -1
- package/services/user-permission.service.d.ts +6 -9
- package/services/user.service.d.ts +3 -0
- package/strategies/jwt.strategy.d.ts +4 -1
- package/validators/password.validator.d.ts +10 -0
- package/cjs/dtos/user-company-permission.dto.js +0 -110
- package/dtos/user-company-permission.dto.d.ts +0 -10
- package/fesm/dtos/user-company-permission.dto.js +0 -98
|
@@ -5,98 +5,57 @@ function _ts_decorate(decorators, target, key, desc) {
|
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
}
|
|
7
7
|
import { CacheModule, UtilsModule } from '@flusys/nestjs-shared/modules';
|
|
8
|
-
import { Module
|
|
8
|
+
import { Module } from '@nestjs/common';
|
|
9
9
|
import { JwtModule } from '@nestjs/jwt';
|
|
10
10
|
import { PassportModule } from '@nestjs/passport';
|
|
11
|
-
import {
|
|
12
|
-
import { AuthConfigService } from '../services/auth-config.service';
|
|
13
|
-
import { AuthDataSourceProvider } from '../services/auth-datasource.provider';
|
|
11
|
+
import { JwtAuthGuard } from '@flusys/nestjs-shared/guards';
|
|
14
12
|
import { AUTH_MODULE_OPTIONS } from '../config/auth.constants';
|
|
15
13
|
import { AuthenticationController, BranchController, CompanyController, CompanySelectionController, UserController, UserPermissionController } from '../controllers';
|
|
16
14
|
import { Company, CompanyBranch, User, UserCompanyPermission } from '../entities';
|
|
17
|
-
import { JwtAuthGuard } from '@flusys/nestjs-shared/guards';
|
|
18
15
|
import { ClearToken } from '../interceptors/clear-token.interceptor';
|
|
19
16
|
import { SetToken } from '../interceptors/set-token.interceptor';
|
|
20
17
|
import { AuthEmailService, AuthenticationService, BranchService, CompanySelectionSessionService, CompanyService, UserPermissionService, UserService } from '../services';
|
|
18
|
+
import { AuthConfigService } from '../services/auth-config.service';
|
|
19
|
+
import { AuthDataSourceProvider } from '../services/auth-datasource.provider';
|
|
21
20
|
import { JwtStrategy } from '../strategies/jwt.strategy';
|
|
22
21
|
export class AuthModule {
|
|
23
22
|
static forRoot(options) {
|
|
24
|
-
|
|
23
|
+
const optionsProvider = {
|
|
24
|
+
provide: AUTH_MODULE_OPTIONS,
|
|
25
|
+
useValue: options
|
|
26
|
+
};
|
|
27
|
+
return this.buildModule(options, [
|
|
28
|
+
optionsProvider,
|
|
29
|
+
...options.providers ?? []
|
|
30
|
+
]);
|
|
25
31
|
}
|
|
26
|
-
static forRootAsync(
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
// Build imports
|
|
30
|
-
const imports = [
|
|
31
|
-
...externalImports,
|
|
32
|
-
PassportModule.register({
|
|
33
|
-
defaultStrategy: 'jwt'
|
|
34
|
-
}),
|
|
35
|
-
JwtModule.register({}),
|
|
36
|
-
CacheModule,
|
|
37
|
-
UtilsModule
|
|
38
|
-
];
|
|
39
|
-
// Build controllers based on configuration
|
|
40
|
-
const controllers = [];
|
|
41
|
-
if (includeController) {
|
|
42
|
-
// Core authentication controller (always included)
|
|
43
|
-
controllers.push(AuthenticationController);
|
|
44
|
-
// User controller (always included)
|
|
45
|
-
controllers.push(UserController);
|
|
46
|
-
// Company feature controllers (conditional)
|
|
47
|
-
if (enableCompanyFeature) {
|
|
48
|
-
controllers.push(CompanySelectionController, CompanyController, BranchController, UserPermissionController);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
// Build providers
|
|
52
|
-
const providers = [
|
|
32
|
+
static forRootAsync(options) {
|
|
33
|
+
const asyncProviders = this.createAsyncProviders(options);
|
|
34
|
+
return this.buildModule(options, [
|
|
53
35
|
...asyncProviders,
|
|
54
|
-
...
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
JwtAuthGuard,
|
|
61
|
-
AuthenticationService,
|
|
62
|
-
AuthEmailService,
|
|
63
|
-
// Interceptors
|
|
64
|
-
SetToken,
|
|
65
|
-
ClearToken,
|
|
66
|
-
// Core services (always included)
|
|
67
|
-
UserService
|
|
68
|
-
];
|
|
69
|
-
// Company feature services (conditional)
|
|
70
|
-
if (enableCompanyFeature) {
|
|
71
|
-
providers.push(CompanySelectionSessionService, CompanyService, BranchService, UserPermissionService);
|
|
72
|
-
}
|
|
73
|
-
// Build exports
|
|
74
|
-
const exports = [
|
|
75
|
-
PassportModule,
|
|
76
|
-
AUTH_MODULE_OPTIONS,
|
|
77
|
-
AuthConfigService,
|
|
78
|
-
AuthDataSourceProvider,
|
|
79
|
-
JwtStrategy,
|
|
80
|
-
JwtAuthGuard,
|
|
81
|
-
AuthenticationService,
|
|
82
|
-
AuthEmailService,
|
|
83
|
-
UserService
|
|
84
|
-
];
|
|
85
|
-
if (enableCompanyFeature) {
|
|
86
|
-
exports.push(CompanySelectionSessionService, CompanyService, BranchService, UserPermissionService);
|
|
87
|
-
}
|
|
36
|
+
...options.providers ?? []
|
|
37
|
+
], options.imports);
|
|
38
|
+
}
|
|
39
|
+
/** Build the dynamic module with shared logic */ static buildModule(options, providers, externalImports = []) {
|
|
40
|
+
const { global = false, includeController = true, bootstrapAppConfig } = options;
|
|
41
|
+
const enableCompanyFeature = bootstrapAppConfig?.enableCompanyFeature ?? false;
|
|
88
42
|
return {
|
|
89
43
|
module: AuthModule,
|
|
90
44
|
global,
|
|
91
|
-
imports
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
45
|
+
imports: [
|
|
46
|
+
...externalImports,
|
|
47
|
+
...this.buildImports()
|
|
48
|
+
],
|
|
49
|
+
controllers: this.buildControllers(includeController, enableCompanyFeature),
|
|
50
|
+
providers: [
|
|
51
|
+
...providers,
|
|
52
|
+
...this.buildProviders(enableCompanyFeature)
|
|
53
|
+
],
|
|
54
|
+
exports: this.buildExports(enableCompanyFeature)
|
|
95
55
|
};
|
|
96
56
|
}
|
|
97
|
-
static
|
|
98
|
-
|
|
99
|
-
const imports = [
|
|
57
|
+
/** Common module imports */ static buildImports() {
|
|
58
|
+
return [
|
|
100
59
|
PassportModule.register({
|
|
101
60
|
defaultStrategy: 'jwt'
|
|
102
61
|
}),
|
|
@@ -104,42 +63,36 @@ export class AuthModule {
|
|
|
104
63
|
CacheModule,
|
|
105
64
|
UtilsModule
|
|
106
65
|
];
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (includeController)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
controllers.push(CompanySelectionController, CompanyController, BranchController, UserPermissionController);
|
|
117
|
-
}
|
|
66
|
+
}
|
|
67
|
+
/** Build controllers based on feature flags */ static buildControllers(includeController, enableCompanyFeature) {
|
|
68
|
+
if (!includeController) return [];
|
|
69
|
+
const controllers = [
|
|
70
|
+
AuthenticationController,
|
|
71
|
+
UserController
|
|
72
|
+
];
|
|
73
|
+
if (enableCompanyFeature) {
|
|
74
|
+
controllers.push(CompanySelectionController, CompanyController, BranchController, UserPermissionController);
|
|
118
75
|
}
|
|
76
|
+
return controllers;
|
|
77
|
+
}
|
|
78
|
+
/** Core providers (without AUTH_MODULE_OPTIONS - that's added separately) */ static buildProviders(enableCompanyFeature) {
|
|
119
79
|
const providers = [
|
|
120
|
-
{
|
|
121
|
-
provide: AUTH_MODULE_OPTIONS,
|
|
122
|
-
useValue: options
|
|
123
|
-
},
|
|
124
|
-
...additionalProviders,
|
|
125
|
-
// Config & Infrastructure
|
|
126
80
|
AuthConfigService,
|
|
127
81
|
AuthDataSourceProvider,
|
|
128
|
-
// Auth core
|
|
129
82
|
JwtStrategy,
|
|
130
83
|
JwtAuthGuard,
|
|
131
84
|
AuthenticationService,
|
|
132
85
|
AuthEmailService,
|
|
133
|
-
// Interceptors
|
|
134
86
|
SetToken,
|
|
135
87
|
ClearToken,
|
|
136
|
-
// Core services (always included)
|
|
137
88
|
UserService
|
|
138
89
|
];
|
|
139
|
-
// Company feature services (conditional)
|
|
140
90
|
if (enableCompanyFeature) {
|
|
141
91
|
providers.push(CompanySelectionSessionService, CompanyService, BranchService, UserPermissionService);
|
|
142
92
|
}
|
|
93
|
+
return providers;
|
|
94
|
+
}
|
|
95
|
+
/** Build module exports */ static buildExports(enableCompanyFeature) {
|
|
143
96
|
const exports = [
|
|
144
97
|
PassportModule,
|
|
145
98
|
AUTH_MODULE_OPTIONS,
|
|
@@ -154,16 +107,9 @@ export class AuthModule {
|
|
|
154
107
|
if (enableCompanyFeature) {
|
|
155
108
|
exports.push(CompanySelectionSessionService, CompanyService, BranchService, UserPermissionService);
|
|
156
109
|
}
|
|
157
|
-
return
|
|
158
|
-
module: AuthModule,
|
|
159
|
-
global,
|
|
160
|
-
imports,
|
|
161
|
-
controllers,
|
|
162
|
-
providers,
|
|
163
|
-
exports
|
|
164
|
-
};
|
|
110
|
+
return exports;
|
|
165
111
|
}
|
|
166
|
-
static createAsyncProviders(options) {
|
|
112
|
+
/** Create async providers for forRootAsync */ static createAsyncProviders(options) {
|
|
167
113
|
const staticOptions = {
|
|
168
114
|
global: options.global,
|
|
169
115
|
includeController: options.includeController,
|
|
@@ -173,13 +119,10 @@ export class AuthModule {
|
|
|
173
119
|
return [
|
|
174
120
|
{
|
|
175
121
|
provide: AUTH_MODULE_OPTIONS,
|
|
176
|
-
useFactory: async (...args)=>{
|
|
177
|
-
const config = await options.useFactory(...args);
|
|
178
|
-
return {
|
|
122
|
+
useFactory: async (...args)=>({
|
|
179
123
|
...staticOptions,
|
|
180
|
-
config
|
|
181
|
-
}
|
|
182
|
-
},
|
|
124
|
+
config: await options.useFactory(...args)
|
|
125
|
+
}),
|
|
183
126
|
inject: options.inject || []
|
|
184
127
|
}
|
|
185
128
|
];
|
|
@@ -192,13 +135,10 @@ export class AuthModule {
|
|
|
192
135
|
},
|
|
193
136
|
{
|
|
194
137
|
provide: AUTH_MODULE_OPTIONS,
|
|
195
|
-
useFactory: async (factory)=>{
|
|
196
|
-
const config = await factory.createAuthOptions();
|
|
197
|
-
return {
|
|
138
|
+
useFactory: async (factory)=>({
|
|
198
139
|
...staticOptions,
|
|
199
|
-
config
|
|
200
|
-
}
|
|
201
|
-
},
|
|
140
|
+
config: await factory.createAuthOptions()
|
|
141
|
+
}),
|
|
202
142
|
inject: [
|
|
203
143
|
options.useClass
|
|
204
144
|
]
|
|
@@ -209,13 +149,10 @@ export class AuthModule {
|
|
|
209
149
|
return [
|
|
210
150
|
{
|
|
211
151
|
provide: AUTH_MODULE_OPTIONS,
|
|
212
|
-
useFactory: async (factory)=>{
|
|
213
|
-
const config = await factory.createAuthOptions();
|
|
214
|
-
return {
|
|
152
|
+
useFactory: async (factory)=>({
|
|
215
153
|
...staticOptions,
|
|
216
|
-
config
|
|
217
|
-
}
|
|
218
|
-
},
|
|
154
|
+
config: await factory.createAuthOptions()
|
|
155
|
+
}),
|
|
219
156
|
inject: [
|
|
220
157
|
options.useExisting
|
|
221
158
|
]
|
|
@@ -229,35 +166,14 @@ export class AuthModule {
|
|
|
229
166
|
}
|
|
230
167
|
];
|
|
231
168
|
}
|
|
232
|
-
/**
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
* Note: Must be REQUEST-scoped since AuthDataSourceProvider is REQUEST-scoped
|
|
236
|
-
*/ static getRepositoryProviders(enableCompanyFeature) {
|
|
237
|
-
const entities = this.getEntities(enableCompanyFeature);
|
|
238
|
-
return entities.map((entity)=>({
|
|
239
|
-
provide: getRepositoryToken(entity),
|
|
240
|
-
scope: Scope.REQUEST,
|
|
241
|
-
useFactory: async (dataSourceProvider)=>{
|
|
242
|
-
return await dataSourceProvider.getRepository(entity);
|
|
243
|
-
},
|
|
244
|
-
inject: [
|
|
245
|
-
AuthDataSourceProvider
|
|
246
|
-
]
|
|
247
|
-
}));
|
|
248
|
-
}
|
|
249
|
-
static getEntities(enableCompanyFeature = false) {
|
|
250
|
-
const coreEntities = [
|
|
251
|
-
User
|
|
252
|
-
];
|
|
253
|
-
const companyEntities = enableCompanyFeature ? [
|
|
169
|
+
/** Get auth entities based on feature flags */ static getEntities(enableCompanyFeature = false) {
|
|
170
|
+
return enableCompanyFeature ? [
|
|
171
|
+
User,
|
|
254
172
|
Company,
|
|
255
173
|
CompanyBranch,
|
|
256
174
|
UserCompanyPermission
|
|
257
|
-
] : [
|
|
258
|
-
|
|
259
|
-
...coreEntities,
|
|
260
|
-
...companyEntities
|
|
175
|
+
] : [
|
|
176
|
+
User
|
|
261
177
|
];
|
|
262
178
|
}
|
|
263
179
|
}
|
|
@@ -25,30 +25,43 @@ function _ts_param(paramIndex, decorator) {
|
|
|
25
25
|
decorator(target, key, paramIndex);
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
|
-
import { Inject, Injectable, Optional } from '@nestjs/common';
|
|
29
|
-
import { AuthModuleOptions } from '../interfaces/auth-module-options.interface';
|
|
28
|
+
import { Inject, Injectable, InternalServerErrorException, Logger, Optional } from '@nestjs/common';
|
|
30
29
|
import { AUTH_MODULE_OPTIONS } from '../config/auth.constants';
|
|
30
|
+
import { AuthModuleOptions } from '../interfaces/auth-module-options.interface';
|
|
31
31
|
export class AuthConfigService {
|
|
32
|
-
// Database Mode
|
|
33
|
-
|
|
32
|
+
// Database Mode (required by IModuleConfigService)
|
|
33
|
+
getDatabaseMode() {
|
|
34
34
|
return this.options.bootstrapAppConfig?.databaseMode ?? 'single';
|
|
35
35
|
}
|
|
36
|
-
|
|
36
|
+
isMultiTenant() {
|
|
37
37
|
return this.getDatabaseMode() === 'multi-tenant';
|
|
38
38
|
}
|
|
39
39
|
// Feature Flags
|
|
40
|
-
|
|
40
|
+
isCompanyFeatureEnabled() {
|
|
41
41
|
return this.options.bootstrapAppConfig?.enableCompanyFeature ?? false;
|
|
42
42
|
}
|
|
43
43
|
// JWT Configuration
|
|
44
44
|
/** Get JWT secret key */ getJwtSecret() {
|
|
45
|
-
|
|
45
|
+
if (!this.config.jwtSecret) {
|
|
46
|
+
throw new InternalServerErrorException('[AuthModule] JWT_SECRET is required - set via environment or module config');
|
|
47
|
+
}
|
|
48
|
+
return this.config.jwtSecret;
|
|
46
49
|
}
|
|
47
50
|
/** Get JWT expiration time */ getJwtExpiration() {
|
|
48
51
|
return this.config.jwtExpiration ?? '1h';
|
|
49
52
|
}
|
|
50
53
|
/** Get refresh token secret key */ getRefreshTokenSecret() {
|
|
51
|
-
|
|
54
|
+
if (this.config.refreshTokenSecret) {
|
|
55
|
+
return this.config.refreshTokenSecret;
|
|
56
|
+
}
|
|
57
|
+
if (process.env.NODE_ENV === 'production') {
|
|
58
|
+
throw new InternalServerErrorException('[AuthModule] REFRESH_TOKEN_SECRET is required in production - ' + 'using the same secret for access and refresh tokens is a security risk');
|
|
59
|
+
}
|
|
60
|
+
if (!this.refreshTokenSecretWarned) {
|
|
61
|
+
this.refreshTokenSecretWarned = true;
|
|
62
|
+
this.logger.warn('REFRESH_TOKEN_SECRET not configured - using JWT_SECRET as fallback. ' + 'For better security, configure a separate refresh token secret.');
|
|
63
|
+
}
|
|
64
|
+
return this.getJwtSecret();
|
|
52
65
|
}
|
|
53
66
|
/** Get refresh token expiration time */ getRefreshTokenExpiration() {
|
|
54
67
|
return this.config.refreshTokenExpiration ?? '7d';
|
|
@@ -60,30 +73,11 @@ export class AuthConfigService {
|
|
|
60
73
|
/** Get frontend URL for email links */ getFrontendUrl() {
|
|
61
74
|
return this.config.frontendUrl;
|
|
62
75
|
}
|
|
63
|
-
// Database Configuration
|
|
64
|
-
/** Get default database config */ getDefaultDatabaseConfig() {
|
|
65
|
-
return this.config.defaultDatabaseConfig ?? this.config.tenantDefaultDatabaseConfig;
|
|
66
|
-
}
|
|
67
|
-
/** Get tenant default database config (for multi-tenant) */ getTenantDefaultDatabaseConfig() {
|
|
68
|
-
return this.config.tenantDefaultDatabaseConfig;
|
|
69
|
-
}
|
|
70
|
-
/** Get registered tenants */ getTenants() {
|
|
71
|
-
return this.config.tenants ?? [];
|
|
72
|
-
}
|
|
73
|
-
// Raw Options
|
|
74
|
-
/** Get full module options */ getOptions() {
|
|
75
|
-
return {
|
|
76
|
-
...this.options
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
/** Get runtime config */ getConfig() {
|
|
80
|
-
return {
|
|
81
|
-
...this.config
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
76
|
constructor(injectedOptions){
|
|
77
|
+
_define_property(this, "logger", new Logger(AuthConfigService.name));
|
|
85
78
|
_define_property(this, "options", void 0);
|
|
86
79
|
_define_property(this, "config", void 0);
|
|
80
|
+
_define_property(this, "refreshTokenSecretWarned", false);
|
|
87
81
|
this.options = injectedOptions ?? {
|
|
88
82
|
global: false,
|
|
89
83
|
includeController: true
|
|
@@ -26,7 +26,7 @@ function _ts_param(paramIndex, decorator) {
|
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
28
|
import { MultiTenantDataSourceService } from '@flusys/nestjs-shared/modules';
|
|
29
|
-
import { Inject, Injectable, Logger, Optional, Scope } from '@nestjs/common';
|
|
29
|
+
import { Inject, Injectable, InternalServerErrorException, Logger, Optional, Scope } from '@nestjs/common';
|
|
30
30
|
import { REQUEST } from '@nestjs/core';
|
|
31
31
|
import { Request } from 'express';
|
|
32
32
|
import { AuthModuleOptions } from '../interfaces/auth-module-options.interface';
|
|
@@ -41,7 +41,7 @@ export class AuthDataSourceProvider extends MultiTenantDataSourceService {
|
|
|
41
41
|
const lockPromise = (async ()=>{
|
|
42
42
|
const config = this.getDefaultDatabaseConfig();
|
|
43
43
|
if (!config) {
|
|
44
|
-
throw new
|
|
44
|
+
throw new InternalServerErrorException('Default database config is not available');
|
|
45
45
|
}
|
|
46
46
|
const ds = await this.createDataSourceFromConfig(config);
|
|
47
47
|
AuthDataSourceProvider.singleDataSource = ds;
|
|
@@ -90,40 +90,28 @@ export class AuthDataSourceProvider extends MultiTenantDataSourceService {
|
|
|
90
90
|
};
|
|
91
91
|
}
|
|
92
92
|
// Feature Flags
|
|
93
|
-
/**
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
/** Get enable company feature for specific tenant. Falls back to global setting. */ getEnableCompanyFeatureForTenant(tenant) {
|
|
97
|
-
return tenant?.enableCompanyFeature ?? this.getEnableCompanyFeature();
|
|
98
|
-
}
|
|
99
|
-
/** Get enable company feature for current request context */ getEnableCompanyFeatureForCurrentTenant() {
|
|
100
|
-
return this.getEnableCompanyFeatureForTenant(this.getCurrentTenant() ?? undefined);
|
|
93
|
+
/** Check if company feature is enabled (tenant-specific or global fallback) */ isCompanyFeatureEnabled(tenant) {
|
|
94
|
+
const effectiveTenant = tenant ?? this.getCurrentTenant();
|
|
95
|
+
return effectiveTenant?.enableCompanyFeature ?? this.authOptions.bootstrapAppConfig?.enableCompanyFeature ?? false;
|
|
101
96
|
}
|
|
102
97
|
// Entity Management
|
|
103
|
-
/** Get auth entities based on company feature flag */ async getAuthEntities(
|
|
104
|
-
const enable = enableCompanyFeature ?? this.getEnableCompanyFeature();
|
|
98
|
+
/** Get auth entities based on company feature flag */ async getAuthEntities() {
|
|
105
99
|
const { Company, CompanyBranch, User, UserCompanyPermission, UserToken, PendingRegistration } = await import('../entities');
|
|
106
100
|
const coreEntities = [
|
|
107
101
|
User,
|
|
108
102
|
UserToken,
|
|
109
103
|
PendingRegistration
|
|
110
104
|
];
|
|
111
|
-
|
|
105
|
+
return this.isCompanyFeatureEnabled() ? [
|
|
106
|
+
...coreEntities,
|
|
112
107
|
Company,
|
|
113
108
|
CompanyBranch,
|
|
114
109
|
UserCompanyPermission
|
|
115
|
-
] :
|
|
116
|
-
return [
|
|
117
|
-
...coreEntities,
|
|
118
|
-
...companyEntities
|
|
119
|
-
];
|
|
110
|
+
] : coreEntities;
|
|
120
111
|
}
|
|
121
112
|
// Overrides
|
|
122
113
|
/** Override to dynamically set entities based on tenant config */ async createDataSourceFromConfig(config) {
|
|
123
|
-
|
|
124
|
-
const enableCompanyFeature = this.getEnableCompanyFeatureForTenant(currentTenant ?? undefined);
|
|
125
|
-
const entities = await this.getAuthEntities(enableCompanyFeature);
|
|
126
|
-
return super.createDataSourceFromConfig(config, entities);
|
|
114
|
+
return super.createDataSourceFromConfig(config, await this.getAuthEntities());
|
|
127
115
|
}
|
|
128
116
|
constructor(authOptions, request){
|
|
129
117
|
super(AuthDataSourceProvider.buildParentOptions(authOptions), request), _define_property(this, "authOptions", void 0), _define_property(this, "logger", void 0), this.authOptions = authOptions, this.logger = new Logger(AuthDataSourceProvider.name);
|