Angular: найти решение, чтобы узнать разницу между refre sh браузера и закрытием браузера / вкладки? - PullRequest
0 голосов
/ 06 августа 2020

Я пытаюсь найти решение для обработки выхода из системы, когда пользователь закрывает браузер или вкладку, но не при обновлении страницы. Вот моя текущая реализация:

//Handles the browser close event to end user session
  @HostListener('window:beforeunload', ['$event'])
  doSomething($event: any) {
    console.log('beforeunloaded called');
    // Do not redirect to logout page if the SignOff button is clicked from the navigation
    if (this.sessionDataStore == null || !this.sessionDataStore.logoutClicked) {
      this.router.navigate(['/logout']).then(s => { console.log('Logout page executed, sesssion ended and OIDC session logged out') });
      console.log(window.navigator.userAgent);
    }
    // Internet Explorer, have to display the popup for the logic to work properly
    if (/msie\s|trident\/|edge\/|Edg\//i.test(window.navigator.userAgent)) {
      console.log('IE or edge');
      if (this.sessionDataStore == null || !this.sessionDataStore.logoutClicked) {
        $event.returnValue = "Please click 'Leave this page' to close the browser.";
      }
    }
    return 'onbeforeunload';
  }

Моя текущая реализация отлично подходит для закрытия браузера / вкладки. Но по-прежнему выполняет выход пользователя из системы на refre sh.

Я слышал об обходном пути, определяя положение мыши при срабатывании события выгрузки, но не могу найти хорошего ответа.

...