Sharkdevs
Sharkdevs

Reputation: 31

How can I make my application logout when the browser is closed?

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

Answers (2)

Chirag
Chirag

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

Michael Kang
Michael Kang

Reputation: 52847

Try this:

@Component({ 
  ...
)}
class MyComponent {
  @HostListener('window:beforeunload')
  logout() {
     ...
  }
}

Upvotes: 7

Related Questions