clear token when browser is closed in angular 2

Viewed 8672

In Angular 2 application we store the token in local storage, now i want to clear the token on browser closing when user not check the remember me option during login. i did it through unload browser event but problem with that event is not fire when user close the browser from task manager in window.

3 Answers

The below code only remove token on close and not when the page refreshed

 @HostListener('window:unload', ['$event'])
  async unloadHandler(event) {
    if (event.currentTarget.performance.navigation.type !== PerformanceNavigation.TYPE_RELOAD) {
      localStorage.clear();
    }
  }
Related