@propmix/profet-common-header 3.2.0-beta.2 → 3.2.0-beta.4

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.
@@ -464,6 +464,8 @@ class HeaderComponent {
464
464
  this._snackbar = inject(MatSnackBar);
465
465
  this._headerSer = inject(CommonHeaderService);
466
466
  this._domSanitizer = inject(DomSanitizer);
467
+ this.componentInitTime = 0;
468
+ this.lastProcessedLogoutTimestamp = 0;
467
469
  this.INACTIVITY_LIMIT = 30 * 60 * 1000; // 30 minutes
468
470
  this.logoutEvent = new EventEmitter();
469
471
  this.companyControl = new FormControl();
@@ -502,9 +504,15 @@ class HeaderComponent {
502
504
  // },
503
505
  // });
504
506
  // }
505
- this.resetTimer();
506
- // Clear any stale logout signal on init
507
- this._headerSer.setCookie(this._headerSer.SESSION_EXPIRED_KEY, '', -1);
507
+ // Capture when this component/app was loaded
508
+ this.componentInitTime = Date.now();
509
+ // Remove legacy boolean check or reset if needed, but primarily we rely on timestamps now.
510
+ // We do NOT want to clear the cookie immediately on init if it's a valid persistent signal,
511
+ // but the timestamp check guards against "looping" on reload.
512
+ // However, if we want to support "fresh login means fresh session", we might just ignore old timestamps (which we do via > initTime check).
513
+ // So distinct cleanup isn't strictly necessary if the login page no longer deletes it.
514
+ // But let's leave it as is or remove it. The user's issue was "delete cookie" logic on LOGIN PAGE caused race condition.
515
+ // Here we just want to start monitoring.
508
516
  this.startLogoutCheck();
509
517
  }
510
518
  resetTimer(isUserActivity = true) {
@@ -539,16 +547,26 @@ class HeaderComponent {
539
547
  startLogoutCheck() {
540
548
  // Poll for the logout signal cookie (works across ports/subdomains)
541
549
  this.logoutCheckInterval = setInterval(() => {
542
- if (this._headerSer.getCookie(this._headerSer.SESSION_EXPIRED_KEY)) {
543
- // Check if tab still "active" according to the shared time
544
- // If tab is active but receiving a logout signal, it implies a MANUAL logout from another tab.
545
- // If tab is inactive and receiving a logout signal, it implies a TIMEOUT.
546
- const lastActive = this._headerSer.getCookie(this._headerSer.LAST_ACTIVE_SESSION_KEY);
547
- const now = Date.now();
548
- const lastActiveTime = lastActive ? parseInt(lastActive, 10) : 0;
549
- const elapsed = now - lastActiveTime;
550
- const isManual = elapsed < this.INACTIVITY_LIMIT;
551
- this.handleLogout(false, false, isManual);
550
+ const logoutCookie = this._headerSer.getCookie(this._headerSer.SESSION_EXPIRED_KEY);
551
+ if (logoutCookie) {
552
+ const logoutTimestamp = parseInt(logoutCookie, 10);
553
+ // 1. Check if it's a valid number (legacy 'true' string support is irrelevant if we fully switch, but safe to check NaN)
554
+ if (!isNaN(logoutTimestamp)) {
555
+ // 2. Check if this logout event happened AFTER this app was initialized
556
+ // This prevents infinite loops if the user just logged in and the cookie is still there.
557
+ if (logoutTimestamp > this.componentInitTime) {
558
+ // 3. Check if we haven't already processed this exact logout event
559
+ if (logoutTimestamp > this.lastProcessedLogoutTimestamp) {
560
+ this.lastProcessedLogoutTimestamp = logoutTimestamp;
561
+ const lastActive = this._headerSer.getCookie(this._headerSer.LAST_ACTIVE_SESSION_KEY);
562
+ const now = Date.now();
563
+ const lastActiveTime = lastActive ? parseInt(lastActive, 10) : 0;
564
+ const elapsed = now - lastActiveTime;
565
+ const isManual = elapsed < this.INACTIVITY_LIMIT;
566
+ this.handleLogout(false, false, isManual);
567
+ }
568
+ }
569
+ }
552
570
  }
553
571
  }, 2000); // Check every 2 seconds
554
572
  }
@@ -558,14 +576,17 @@ class HeaderComponent {
558
576
  // return;
559
577
  // }
560
578
  if (broadcast) {
561
- // Set a cookie to signal other tabs/ports
562
- this._headerSer.setCookie(this._headerSer.SESSION_EXPIRED_KEY, 'true', 1);
579
+ // Set a cookie to signal other tabs/ports with the current TIMESTAMP
580
+ // This timestamp allows other tabs to know if this is a "new" logout event relative to their session start.
581
+ const now = Date.now();
582
+ this.lastProcessedLogoutTimestamp = now; // Don't logout myself again based on this
583
+ this._headerSer.setCookie(this._headerSer.SESSION_EXPIRED_KEY, now.toString(), 1);
563
584
  }
564
585
  this.logoutEvent.emit();
565
586
  let sessionUrl = this._headerSer.headerConfig.signOutUrl;
566
587
  if (this._headerSer.headerConfig.enableLastUrlRedirection) {
567
588
  const separator = sessionUrl.includes('?') ? '&' : '?';
568
- sessionUrl = sessionUrl + separator + 'returnUrl=' + encodeURIComponent(window.location.href);
589
+ sessionUrl = sessionUrl + separator + 'redirectUrl=' + encodeURIComponent(window.location.href);
569
590
  }
570
591
  // Only add sessionExpired params if it is NOT a manual logout
571
592
  if (!isManual) {
@@ -680,7 +701,9 @@ class HeaderComponent {
680
701
  onLogoutClick() {
681
702
  this.menuTrigger.closeMenu();
682
703
  // Sync with other tabs
683
- this._headerSer.setCookie(this._headerSer.SESSION_EXPIRED_KEY, 'true', 1);
704
+ const now = Date.now();
705
+ this.lastProcessedLogoutTimestamp = now;
706
+ this._headerSer.setCookie(this._headerSer.SESSION_EXPIRED_KEY, now.toString(), 1);
684
707
  // Clear timers
685
708
  if (this.inactivityTimeout)
686
709
  clearTimeout(this.inactivityTimeout);
@@ -688,10 +711,10 @@ class HeaderComponent {
688
711
  clearInterval(this.logoutCheckInterval);
689
712
  this.logoutEvent.emit();
690
713
  let sessionUrl = this._headerSer.headerConfig.signOutUrl;
691
- if (this._headerSer.headerConfig.enableLastUrlRedirection) {
692
- const separator = sessionUrl.includes('?') ? '&' : '?';
693
- sessionUrl = sessionUrl + separator + 'returnUrl=' + encodeURIComponent(window.location.href);
694
- }
714
+ // if (this._headerSer.headerConfig.enableLastUrlRedirection) {
715
+ // const separator = sessionUrl.includes('?') ? '&' : '?';
716
+ // sessionUrl = sessionUrl + separator + 'returnUrl=' + encodeURIComponent(window.location.href);
717
+ // }
695
718
  signOut({ global: true, oauth: { redirectUrl: sessionUrl } })
696
719
  .then((data) => {
697
720
  window.open(sessionUrl, '_self');