How to logout outside of components in react SPA

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 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:

        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 if needed.

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

Kind Regards,
Nik