# User logout on tab/window close

**URL:** <https://community.auth0.com/t/user-logout-on-tab-window-close/126264>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [February 1, 2024, 9:23am UTC](https://community.auth0.com/t/user-logout-on-tab-window-close/126264 "2024-02-01T09:23:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![david47](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@david47](https://community.auth0.com/u/david47)\
**Post date:** [February 1, 2024, 9:23am UTC](https://community.auth0.com/t/user-logout-on-tab-window-close/126264/1 "2024-02-01T09:23:04Z")

</div>

We are building a React application with the auth0 SDK closely following [this tutorial](https://developer.auth0.com/resources/guides/spa/react/basic-authentication). The basic `loginWithRedirect`, `withAuthenticationRequired` and `logout` functions all work as expected.

Since we are dealing with sensitive data in our app we want to make sure to log users out of their session when they close the application tab or the browser window.

To start with, in the Login Session Management settings we make sure to select a Non-Persistent Session, and we set “Inactivity Timeout” and “Require log in after” relatively low.  
As mentioned [here](https://auth0.com/docs/manage-users/sessions/non-persistent-sessions#browser-limitations), this is not enough to trigger a logout for tab closes and neither for all cases of browser closes.

One workaround I have found discussed [here](https://community.auth0.com/t/logout-is-not-happening-on-browser-tab-close/93862) and elsewhere in the forum is to use an event handler for `beforeunload` which calls the logout function (i.e. implements a redirect to the logout endpoint):

```auto
const { logout } = useAuth0();

  useEffect(() => {
    function handleBeforeUnload(event: BeforeUnloadEvent) {
      event.preventDefault();
      event.returnValue = '';
      logout({ logoutParams: {returnTo: window.location.origin} });
      return '';
    };
    
    window.addEventListener('beforeunload', handleBeforeUnload, {capture: true});

    return () => {
      window.removeEventListener('beforeunload', handleBeforeUnload, {capture: true});
    };
  }, []);
  

```

However, this is not a reliable solution and causes unexpected behaviour (e.g. me reaching the rate limit).

I would appreciate any help with implementing this “logout on tab close” behaviour correctly!

---

<div class="post-metadata">

**Author:** ![alex.brett](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alex.brett/32/60157_2.png) [@alex.brett](https://community.auth0.com/u/alex.brett)\
**Post date:** [December 10, 2024, 7:25pm UTC](https://community.auth0.com/t/user-logout-on-tab-window-close/126264/2 "2024-12-10T19:25:05Z")

</div>

Hello @david47!

Using Non-persistent sessions will give you the desired result. Here are some docs that explain how they work in Auth0. Please give this a read

> **[Non-Persistent Sessions](https://auth0.com/docs/manage-users/sessions/non-persistent-sessions)**
>
> Describes non-persistent sessions available with Auth0.

I hope this helps!

Best,  
Alex

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [December 24, 2024, 7:25pm UTC](https://community.auth0.com/t/user-logout-on-tab-window-close/126264/3 "2024-12-24T19:25:47Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
