Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear token when browser is closed in angular 2

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.

like image 951
bangash Avatar asked Jun 24 '26 17:06

bangash


1 Answers

The following should accomplish this...

import { Component, HostListener } from "@angular/core";

@Component({
    selector: 'app-root',
    templateUrl:"./app/app.component.html"
})

export class AppComponent {
    @HostListener("window:onbeforeunload",["$event"])
    clearLocalStorage(event){
        localStorage.clear();
    }
}

Note : onBeforeUnload is executing on browser close event

like image 148
Shahid Ahmad Avatar answered Jun 27 '26 05:06

Shahid Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!