2

logout the user when he close the browser not on refresh

@HostListener('window:beforeunload', [ '$event' ]) unloadHandler(event) { this.logout() }

logout() { localStorage.removeItem('id'); this.router.navigate(["/login"]); }

I use this, it works but the user also logout on refresh the browser

Balaji
  • 21
  • 1
  • 5

1 Answers1

1

In your app.component.ts

Add before your constructor(),

@HostListener('window:beforeunload')
  unloadHandler(event) {
    localStorage.removeItem('currentUser');
  }
Sachin from Pune
  • 656
  • 8
  • 19
  • 1
    If I use this it is also working on page refresh, I need this function to work only when the user close the browser or tab – Balaji Mar 22 '19 at 10:03
  • please go through this link https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing – Sachin from Pune Mar 22 '19 at 11:42