@opensourcekd/ng-common-libs 2.0.4 → 2.0.6

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 CHANGED
@@ -95,6 +95,10 @@ import {
95
95
  AuthService,
96
96
  EventBus,
97
97
 
98
+ // Helper Functions
99
+ configureAuth0,
100
+ createAuthService,
101
+
98
102
  // Configuration
99
103
  APP_CONFIG,
100
104
  AUTH0_CONFIG,
@@ -111,6 +115,7 @@ import {
111
115
  UserData,
112
116
  EventPayload,
113
117
  Auth0Config,
118
+ Auth0ConfigOptions,
114
119
  StorageConfig,
115
120
  StorageKeys,
116
121
  AppState,
package/dist/index.cjs CHANGED
@@ -12,6 +12,9 @@ var operators = require('rxjs/operators');
12
12
  * // Create an instance
13
13
  * const eventBus = new EventBus();
14
14
  *
15
+ * // Create an instance with an identifier
16
+ * const eventBus = new EventBus({ id: 'MFE' });
17
+ *
15
18
  * // Emit an event
16
19
  * eventBus.emit('user:login', { userId: '123', username: 'john' });
17
20
  *
@@ -19,10 +22,28 @@ var operators = require('rxjs/operators');
19
22
  * eventBus.on('user:login').subscribe(data => {
20
23
  * console.log('User logged in:', data);
21
24
  * });
25
+ *
26
+ * // Get the identifier
27
+ * const id = eventBus.getId(); // 'MFE' or undefined
22
28
  * ```
23
29
  */
24
30
  class EventBus {
25
31
  eventSubject = new rxjs.Subject();
32
+ id;
33
+ /**
34
+ * Create a new EventBus instance
35
+ * @param options - Optional configuration with an id to identify the instance
36
+ */
37
+ constructor(options) {
38
+ this.id = options?.id;
39
+ }
40
+ /**
41
+ * Get the identifier of this EventBus instance
42
+ * @returns The id if provided during initialization, undefined otherwise
43
+ */
44
+ getId() {
45
+ return this.id;
46
+ }
26
47
  /**
27
48
  * Emit an event with optional data
28
49
  * @param eventType - The type/name of the event
@@ -88,20 +109,47 @@ const APP_CONFIG = {
88
109
  */
89
110
  /**
90
111
  * Auth0 client configuration
91
- * Override these values in your consuming application by setting them before importing AuthService
112
+ * Override these values in your consuming application using configureAuth0()
92
113
  *
93
114
  * Note: redirectUri defaults to window.location.origin (base URL without path).
94
115
  * Auth0 will redirect back to this URL after authentication.
95
116
  */
96
117
  const AUTH0_CONFIG = {
97
- domain: '', // Set in consuming app: process.env['NX_AUTH0_DOMAIN'] || 'your-domain.auth0.com'
98
- clientId: '', // Set in consuming app: process.env['NX_AUTH0_CLIENT_ID'] || 'your-client-id'
118
+ domain: '', // Set via configureAuth0() or process.env
119
+ clientId: '', // Set via configureAuth0() or process.env
99
120
  redirectUri: typeof window !== 'undefined' ? window.location.origin : '',
100
121
  logoutUri: typeof window !== 'undefined' ? window.location.origin : '',
101
- audience: '', // Optional: Set in consuming app if using API authorization
122
+ audience: '', // Optional: Set via configureAuth0()
102
123
  scope: 'openid profile email', // Default scopes
103
- connection: undefined, // Optional: Force specific connection (e.g., 'Username-Password-Authentication')
124
+ connection: undefined, // Optional: Force specific connection
104
125
  };
126
+ /**
127
+ * Configure Auth0 settings
128
+ * Call this function early in your application (e.g., in app.config.ts or main.ts)
129
+ * to override the default Auth0 configuration values.
130
+ *
131
+ * @param config - Partial Auth0 configuration to override defaults
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * import { configureAuth0, APP_CONFIG } from '@opensourcekd/ng-common-libs';
136
+ *
137
+ * configureAuth0({
138
+ * domain: APP_CONFIG.auth0Domain,
139
+ * clientId: APP_CONFIG.auth0ClientId,
140
+ * audience: APP_CONFIG.apiUrl,
141
+ * });
142
+ * ```
143
+ */
144
+ function configureAuth0(config) {
145
+ // Only update properties that are explicitly provided (not undefined)
146
+ Object.keys(config).forEach((key) => {
147
+ const configKey = key;
148
+ if (config[configKey] !== undefined) {
149
+ AUTH0_CONFIG[configKey] = config[configKey];
150
+ }
151
+ });
152
+ }
105
153
  /**
106
154
  * Storage configuration
107
155
  * Controls where sensitive data is stored
@@ -117,6 +165,20 @@ const STORAGE_KEYS = {
117
165
  ACCESS_TOKEN: 'auth0_access_token',
118
166
  USER_INFO: 'auth0_user_info',
119
167
  };
168
+ /**
169
+ * Reset AUTH0_CONFIG to default values
170
+ * Useful for testing
171
+ * @internal
172
+ */
173
+ function resetAuth0Config() {
174
+ AUTH0_CONFIG.domain = '';
175
+ AUTH0_CONFIG.clientId = '';
176
+ AUTH0_CONFIG.redirectUri = typeof window !== 'undefined' ? window.location.origin : '';
177
+ AUTH0_CONFIG.logoutUri = typeof window !== 'undefined' ? window.location.origin : '';
178
+ AUTH0_CONFIG.audience = '';
179
+ AUTH0_CONFIG.scope = 'openid profile email';
180
+ AUTH0_CONFIG.connection = undefined;
181
+ }
120
182
  /**
121
183
  * Helper functions for storage operations
122
184
  * These work with both localStorage and sessionStorage
@@ -204,10 +266,16 @@ function removeStorageItem(key, storageType = 'sessionStorage') {
204
266
  * };
205
267
  * const authService = new AuthService(authConfig, eventBus);
206
268
  *
269
+ * // Or create with an identifier
270
+ * const authService = new AuthService(authConfig, eventBus, undefined, undefined, { id: 'MFE' });
271
+ *
207
272
  * // Use the service
208
273
  * await authService.login();
209
274
  * const user = authService.getUser();
210
275
  * const token = await authService.getToken();
276
+ *
277
+ * // Get the identifier
278
+ * const id = authService.getId(); // 'MFE' or undefined
211
279
  * ```
212
280
  */
213
281
  class AuthService {
@@ -226,12 +294,14 @@ class AuthService {
226
294
  storageConfig;
227
295
  storageKeys;
228
296
  eventBus;
297
+ id;
229
298
  /**
230
299
  * Create a new AuthService instance
231
300
  * @param config - Auth0 configuration
232
301
  * @param eventBus - EventBus instance for emitting auth events
233
302
  * @param storageConfig - Storage configuration (optional, defaults to sessionStorage)
234
303
  * @param storageKeys - Storage keys (optional, defaults to standard keys)
304
+ * @param options - Optional configuration with an id to identify the instance
235
305
  */
236
306
  constructor(config, eventBus, storageConfig = {
237
307
  TOKEN_STORAGE: 'sessionStorage',
@@ -239,16 +309,24 @@ class AuthService {
239
309
  }, storageKeys = {
240
310
  ACCESS_TOKEN: 'auth0_access_token',
241
311
  USER_INFO: 'auth0_user_info'
242
- }) {
312
+ }, options) {
243
313
  this.config = config;
244
314
  this.eventBus = eventBus;
245
315
  this.storageConfig = storageConfig;
246
316
  this.storageKeys = storageKeys;
317
+ this.id = options?.id;
247
318
  this.userSubject = new rxjs.BehaviorSubject(this.getUserInfoFromStorage());
248
319
  this.user$ = this.userSubject.asObservable();
249
320
  console.log("[AuthService] AuthService instance created (Auth0 client will be initialized on first use)");
250
321
  // Lazy initialization - Auth0 client will be initialized in ensureInitialized() on first use
251
322
  }
323
+ /**
324
+ * Get the identifier of this AuthService instance
325
+ * @returns The id if provided during initialization, undefined otherwise
326
+ */
327
+ getId() {
328
+ return this.id;
329
+ }
252
330
  /**
253
331
  * Initialize Auth0 client
254
332
  */
@@ -588,6 +666,43 @@ class AuthService {
588
666
  console.log('[AuthService] Auth event emitted:', event.type);
589
667
  }
590
668
  }
669
+ /**
670
+ * Create AuthService instance using AUTH0_CONFIG
671
+ * Helper function for creating AuthService with default configuration from AUTH0_CONFIG
672
+ *
673
+ * Note: Make sure to call configureAuth0() before using this helper
674
+ *
675
+ * @param eventBus - EventBus instance for auth events
676
+ * @returns Configured AuthService instance
677
+ *
678
+ * @example
679
+ * ```typescript
680
+ * import { createAuthService, EventBus, configureAuth0, APP_CONFIG } from '@opensourcekd/ng-common-libs';
681
+ *
682
+ * // Configure Auth0 first
683
+ * configureAuth0({
684
+ * domain: APP_CONFIG.auth0Domain,
685
+ * clientId: APP_CONFIG.auth0ClientId,
686
+ * audience: APP_CONFIG.apiUrl,
687
+ * });
688
+ *
689
+ * // Create instances
690
+ * const eventBus = new EventBus();
691
+ * const authService = createAuthService(eventBus);
692
+ * ```
693
+ */
694
+ function createAuthService(eventBus) {
695
+ const auth0Config = {
696
+ domain: AUTH0_CONFIG.domain,
697
+ clientId: AUTH0_CONFIG.clientId,
698
+ redirectUri: AUTH0_CONFIG.redirectUri,
699
+ logoutUri: AUTH0_CONFIG.logoutUri,
700
+ audience: AUTH0_CONFIG.audience,
701
+ scope: AUTH0_CONFIG.scope,
702
+ connection: AUTH0_CONFIG.connection,
703
+ };
704
+ return new AuthService(auth0Config, eventBus, STORAGE_CONFIG, STORAGE_KEYS);
705
+ }
591
706
 
592
707
  exports.APP_CONFIG = APP_CONFIG;
593
708
  exports.AUTH0_CONFIG = AUTH0_CONFIG;
@@ -595,7 +710,10 @@ exports.AuthService = AuthService;
595
710
  exports.EventBus = EventBus;
596
711
  exports.STORAGE_CONFIG = STORAGE_CONFIG;
597
712
  exports.STORAGE_KEYS = STORAGE_KEYS;
713
+ exports.configureAuth0 = configureAuth0;
714
+ exports.createAuthService = createAuthService;
598
715
  exports.getStorageItem = getStorageItem$1;
599
716
  exports.removeStorageItem = removeStorageItem$1;
717
+ exports.resetAuth0Config = resetAuth0Config;
600
718
  exports.setStorageItem = setStorageItem$1;
601
719
  //# sourceMappingURL=index.cjs.map