@brggroup/share-lib 0.0.86 → 0.0.87

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.
@@ -560,6 +560,11 @@ class SessionManagerService {
560
560
  LEADER_KEY = 'APP_SESSION_LEADER';
561
561
  HEARTBEAT_KEY = 'APP_SESSION_HEARTBEAT';
562
562
  ACTIVITY_KEY = 'APP_LAST_ACTIVITY';
563
+ SESSION_EVENT = 'APP_SESSION_EVENT';
564
+ LOGIN_EVENT = 'APP_LOGIN_EVENT';
565
+ LOGOUT_EVENT = 'APP_LOGOUT_EVENT';
566
+ ACTIVITY_EVENT = 'APP_ACTIVITY_EVENT';
567
+ TOKEN_REFRESH_EVENT = 'APP_TOKEN_REFRESH_EVENT';
563
568
  tabId = crypto.randomUUID();
564
569
  isLeader = false;
565
570
  isRunning = false;
@@ -579,6 +584,11 @@ class SessionManagerService {
579
584
  this.updateActivity();
580
585
  this.resetIdle();
581
586
  this.electLeader();
587
+ window.addEventListener('storage', (event) => {
588
+ if (event.key === this.LOGIN_EVENT) {
589
+ location.reload();
590
+ }
591
+ });
582
592
  }
583
593
  stop() {
584
594
  this.isRunning = false;
@@ -600,7 +610,7 @@ class SessionManagerService {
600
610
  return;
601
611
  this.updateActivity();
602
612
  this.resetIdle();
603
- this.broadcast('activity');
613
+ this.broadcast(this.ACTIVITY_EVENT);
604
614
  }, 1000);
605
615
  updateActivity() {
606
616
  localStorage.setItem(this.ACTIVITY_KEY, Date.now().toString());
@@ -675,7 +685,7 @@ class SessionManagerService {
675
685
  try {
676
686
  const res = await this.handlers.refreshToken();
677
687
  if (res?.IsSuccess) {
678
- this.broadcast('token-refresh');
688
+ this.broadcast(this.TOKEN_REFRESH_EVENT);
679
689
  this.scheduleRefresh();
680
690
  }
681
691
  else {
@@ -697,13 +707,16 @@ class SessionManagerService {
697
707
  if (!this.isRunning)
698
708
  return;
699
709
  const type = msg.data?.type;
700
- if (type === 'activity') {
710
+ if (type === this.ACTIVITY_EVENT) {
701
711
  this.resetIdle();
702
712
  }
703
- if (type === 'logout') {
713
+ if (type === this.LOGOUT_EVENT) {
704
714
  this.handlers.logout();
705
715
  }
706
- if (type === 'token-refresh') {
716
+ if (type === this.LOGIN_EVENT) {
717
+ location.reload();
718
+ }
719
+ if (type === this.TOKEN_REFRESH_EVENT) {
707
720
  this.scheduleRefresh();
708
721
  }
709
722
  };
@@ -715,15 +728,21 @@ class SessionManagerService {
715
728
  this.channel.postMessage({ type });
716
729
  }
717
730
  else {
718
- localStorage.setItem('APP_SESSION_EVENT', JSON.stringify({ type, t: Date.now() }));
731
+ localStorage.setItem(this.SESSION_EVENT, JSON.stringify({ type, t: Date.now() }));
719
732
  }
720
733
  }
734
+ broadcastLogin() {
735
+ this.broadcast(this.LOGIN_EVENT);
736
+ }
737
+ broadcastLogout() {
738
+ this.broadcast(this.LOGOUT_EVENT);
739
+ }
721
740
  /* -------------------------------- LOGOUT -------------------------------- */
722
741
  logout() {
723
742
  if (!this.isRunning)
724
743
  return;
725
- this.broadcast('logout');
726
744
  this.handlers.logout();
745
+ this.broadcast(this.LOGOUT_EVENT);
727
746
  }
728
747
  /* -------------------------------- UTILS -------------------------------- */
729
748
  debounce(fn, delay) {
@@ -812,6 +831,7 @@ class AuthService extends HTTPService {
812
831
  logout: () => this.signOut(),
813
832
  getToken: () => this.getToken(),
814
833
  });
834
+ this.sessionManager.broadcastLogin();
815
835
  if (returnUrl) {
816
836
  this.router.navigateByUrl(returnUrl);
817
837
  }
@@ -845,6 +865,7 @@ class AuthService extends HTTPService {
845
865
  ---------------------------------------- */
846
866
  signOut() {
847
867
  TokenStorage.clearToken();
868
+ this.sessionManager.broadcastLogout();
848
869
  this.sessionManager.stop();
849
870
  const currentUrl = this.router.routerState.snapshot.url;
850
871
  this.router.navigate(['/login'], {