Reputation: 31
I am working on a project with Angular7 typescript and a nodejs backend. I want the application to log out the user when they close the browser. How can I achieve this using typescript?
I am using the function below to do my normal signout and it works. How can I call it when the browser closes?
logout(): void {
this.cookieService.delete('jwt_token', '/');
this.cookieService.delete('my_token', '/');
this.isAuthenticated = false;
this.clockSubscription.unsubscribe();
}
Upvotes: 2
Views: 678
Reputation: 413
You can store your token, user and credentials in sessionStorage
rather than localStorage
. sessionStorage
is removed whenever you close tab/browser, localStorage
don't!
Upvotes: 1
Reputation: 52847
Try this:
@Component({
...
)}
class MyComponent {
@HostListener('window:beforeunload')
logout() {
...
}
}
Upvotes: 7