# How to logout outside of components in react SPA

**URL:** <https://community.auth0.com/t/how-to-logout-outside-of-components-in-react-spa/170128>\
**Category:** Get Help\
**Tags:** sdks-quickstarts, auth0-react\
**Created:** [January 24, 2025, 5:45pm UTC](https://community.auth0.com/t/how-to-logout-outside-of-components-in-react-spa/170128 "2025-01-24T17:45:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johan.lindell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/johan.lindell/32/71466_2.png) [@johan.lindell](https://community.auth0.com/u/johan.lindell)\
**Post date:** [January 24, 2025, 5:45pm UTC](https://community.auth0.com/t/how-to-logout-outside-of-components-in-react-spa/170128/1 "2025-01-24T17:45:39Z")

</div>

When our server returns a 401 I’d like to clear all user information and log out of the current session. How can I call logout from an axios interceptor, not from inside a component in React.

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [January 24, 2025, 10:12pm UTC](https://community.auth0.com/t/how-to-logout-outside-of-components-in-react-spa/170128/3 "2025-01-24T22:12:41Z")

</div>

Hi @johan.lindell

Thank you for posting your inquiry on the Auth0 Community!

Regarding logging out the current session after a 401 error, you can review this [community post](https://community.auth0.com/t/log-the-user-out-of-auth0-session-after-getting-401-response-in-nextjs-sdk/82470) regarding the same matter. Even if the post addresses a NodeJS SDK implementation, it should be useful for your use case.

Alternatively, you can try adding the following code in the callback handler:

```auto
        res.redirect(
          `${AUTH0_ISSUER_BASE_URL}/v2/logout?${new URLSearchParams({
            client_id: AUTH0_CLIENT_ID as string,
            returnTo: BASE_URL,
          })}`
        )
        .end();

```

You can also try to catch the 401 error and redirect the user to your `/logout` endpoint as follows:

> window.location.href = “/api/auth/logout”;

Otherwise, whenever you receive a **401 Unauthorized** error, there should not be any user information saved on Auth0 since they did not complete the login process, however you can make a call to you management api in order to [delete the user](https://auth0.com/docs/api/management/v2/users/delete-users-by-id) if needed.

Please let me know if you have any other questions by leaving a reply on the post!

Kind Regards,  
Nik

---

<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:** [February 7, 2025, 10:12pm UTC](https://community.auth0.com/t/how-to-logout-outside-of-components-in-react-spa/170128/4 "2025-02-07T22:12:44Z")

</div>

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