@aakash58/chatbot 1.1.6 → 1.1.7

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.
@@ -1,7 +1,7 @@
1
1
  import * as i1$2 from '@angular/common';
2
2
  import { CommonModule } from '@angular/common';
3
3
  import * as i0 from '@angular/core';
4
- import { isDevMode, Injectable, signal, inject, effect, EventEmitter, Output, Input, Component, Pipe, ViewChild, Inject, HostListener, Directive, InjectionToken, computed, ChangeDetectionStrategy, ChangeDetectorRef, ElementRef, ViewEncapsulation, ContentChildren, makeEnvironmentProviders, APP_INITIALIZER } from '@angular/core';
4
+ import { isDevMode, Injectable, InjectionToken, inject, signal, effect, EventEmitter, Output, Input, Component, Pipe, ViewChild, Inject, HostListener, Directive, computed, ChangeDetectionStrategy, ChangeDetectorRef, ElementRef, ViewEncapsulation, ContentChildren, makeEnvironmentProviders, APP_INITIALIZER } from '@angular/core';
5
5
  import { ReplaySubject, delay, retry, map, catchError, of, BehaviorSubject, firstValueFrom, throwError, tap, Observable, Subject, lastValueFrom, switchMap, filter, take } from 'rxjs';
6
6
  import { JwtHelperService } from '@auth0/angular-jwt';
7
7
  import * as i1 from '@angular/common/http';
@@ -214,6 +214,23 @@ class SnackbarUtils {
214
214
  }
215
215
  }
216
216
 
217
+ /**
218
+ * Injection token for advanced configuration
219
+ */
220
+ const DOOHBOT_ADVANCED_CONFIG = new InjectionToken('DOOHBOT_ADVANCED_CONFIG', {
221
+ providedIn: 'root',
222
+ factory: () => ({
223
+ authMode: 'manual',
224
+ tenantResolution: 'static',
225
+ enableMultiTenant: false,
226
+ persistChatPerTenant: true,
227
+ authHeaderName: 'Authorization',
228
+ authHeaderFormat: 'Bearer {token}',
229
+ enableRoleBasedFeatures: false,
230
+ debugMode: false,
231
+ }),
232
+ });
233
+
217
234
  class StorageService {
218
235
  storageKey;
219
236
  secretKey;
@@ -508,6 +525,7 @@ class AuthService {
508
525
  secretKey;
509
526
  companyCode;
510
527
  jwtHelper = new JwtHelperService();
528
+ config = inject(DOOHBOT_ADVANCED_CONFIG, { optional: true });
511
529
  // Reactive Authentication State
512
530
  authStatusSubject = new BehaviorSubject('unauthenticated');
513
531
  authStatus$ = this.authStatusSubject.asObservable();
@@ -545,11 +563,44 @@ class AuthService {
545
563
  get authStatus() {
546
564
  return this.authStatusSubject.value;
547
565
  }
566
+ /**
567
+ * Resolves a federated token from browser storage
568
+ */
569
+ resolveSilentToken() {
570
+ const commonKeys = ['access_token', 'id_token', 'token', 'authToken'];
571
+ const configuredKey = this.config?.autoConfigFromStorage;
572
+ // Try configured storage key (both localStorage and sessionStorage)
573
+ if (configuredKey) {
574
+ const token = localStorage.getItem(configuredKey) || sessionStorage.getItem(configuredKey);
575
+ if (token) {
576
+ Logger.debug(`[Auth] Resolved token from configured key: ${configuredKey}`);
577
+ return token;
578
+ }
579
+ }
580
+ // Try common fallback keys
581
+ for (const key of commonKeys) {
582
+ const token = localStorage.getItem(key) || sessionStorage.getItem(key);
583
+ if (token) {
584
+ Logger.debug(`[Auth] Resolved token from fallback key: ${key}`);
585
+ return token;
586
+ }
587
+ }
588
+ return null;
589
+ }
548
590
  /**
549
591
  * Authenticate user with federated credentials
550
592
  */
551
- async federatedLogin(credentials, silent = false) {
593
+ async federatedLogin(credentials, silent = true) {
552
594
  Logger.log('>>>>>>>Attempting federated login...');
595
+ // Resolve token if not provided
596
+ if (!credentials.accessToken) {
597
+ credentials.accessToken = this.resolveSilentToken() || '';
598
+ }
599
+ // If still no token, exit gracefully
600
+ if (!credentials.accessToken) {
601
+ Logger.log('[Auth] No federated token found in storage. Exiting silently.');
602
+ return false;
603
+ }
553
604
  this.isLoggingIn.set(true);
554
605
  if (!silent) {
555
606
  this.authError.set(null);
@@ -565,7 +616,7 @@ class AuthService {
565
616
  access_token: response.data.access_token,
566
617
  refresh_token: credentials.rememberMe ? response.data.refresh_token : '',
567
618
  };
568
- this.setSession(authResult, credentials.rememberMe);
619
+ this.setSession(authResult, credentials.rememberMe || false);
569
620
  Logger.log('Federated login successful, authenticated state set to true');
570
621
  if (!silent) {
571
622
  SnackbarUtils.success('Login successful!');
@@ -2473,23 +2524,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.14", ngImpo
2473
2524
  }]
2474
2525
  }], ctorParameters: () => [] });
2475
2526
 
2476
- /**
2477
- * Injection token for advanced configuration
2478
- */
2479
- const DOOHBOT_ADVANCED_CONFIG = new InjectionToken('DOOHBOT_ADVANCED_CONFIG', {
2480
- providedIn: 'root',
2481
- factory: () => ({
2482
- authMode: 'manual',
2483
- tenantResolution: 'static',
2484
- enableMultiTenant: false,
2485
- persistChatPerTenant: true,
2486
- authHeaderName: 'Authorization',
2487
- authHeaderFormat: 'Bearer {token}',
2488
- enableRoleBasedFeatures: false,
2489
- debugMode: false,
2490
- }),
2491
- });
2492
-
2493
2527
  /**
2494
2528
  * ChatUIStateService - Centralized UI state management
2495
2529
  *
@@ -4414,9 +4448,9 @@ class Doohbot extends DoohbotInput {
4414
4448
  if (this.isAuthenticated()) {
4415
4449
  this.initializeUI();
4416
4450
  }
4417
- else if (this.config.authToken) {
4418
- //! Auto-authenticate if authToken is provided on startup
4419
- this.onFederatedLogin(this.config.authToken);
4451
+ else {
4452
+ //! Always attempt silent federated login on startup if not authenticated
4453
+ this.onFederatedLogin();
4420
4454
  }
4421
4455
  }
4422
4456
  initializeUI() {
@@ -4495,11 +4529,12 @@ class Doohbot extends DoohbotInput {
4495
4529
  if (autoKey) {
4496
4530
  this.authService.setLicenseKey(autoKey);
4497
4531
  }
4498
- this.authService.federatedLogin({
4532
+ const request = {
4499
4533
  accessToken: token,
4500
4534
  rememberMe: true,
4501
4535
  license: autoKey,
4502
- });
4536
+ };
4537
+ this.authService.federatedLogin(request);
4503
4538
  }
4504
4539
  performLogout() {
4505
4540
  DialogUtils.confirmLogout(this.dialogService).subscribe((confirmed) => {