# Logging out completely

**URL:** <https://community.auth0.com/t/logging-out-completely/118940>\
**Category:** Get Help\
**Tags:** new-universal-login-experience, login-experience\
**Created:** [October 19, 2023, 5:13pm UTC](https://community.auth0.com/t/logging-out-completely/118940 "2023-10-19T17:13:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![allistair](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/allistair/32/49111_2.png) [@allistair](https://community.auth0.com/u/allistair)\
**Post date:** [October 19, 2023, 5:13pm UTC](https://community.auth0.com/t/logging-out-completely/118940/1 "2023-10-19T17:13:30Z")

</div>

Hey,

I’ve seen a range of folk raising logout-doesn’t-really-logout, I’d like to try again. Failing this I will raise a ticket on the nextjs-auth0 Github project.

I don’t consider my implementation anything other than standard, that is to say:

- Login button with href /api/auth/login?returnTo=/dashboard
- Out of the box middleware.ts for protecting /dashboard/\*
- A logout button with href /api/auth/logout

If I click my Login button, I’ll go to the Auth0 universal login, and get redirected to my /dashboard page, with all the user hooks working. Excellent. I can see a cookie called `appSession`.

If I click logout, I get taken back to my / login page. I can see the `appSession` cookie is removed.

BUT

If I tap login, to go to /api/auth/login?returnTo=/dashboard then I am automatically logged into /dashboard, without the Auth0 universal login.

This is challenging, because I am trying to test with many users in development, so I need to be able to properly logout. It’s also IMO a security risk if this happens in production, as if I want to logout my session, someone in a coffee shop should not be able to tap login and expect to be logged in.

One thing that DOES work is overwriting the /api/auth/[auth0]/route.ts as follows:

```auto
import { handleAuth, handleLogout } from "@auth0/nextjs-auth0";
import { NextResponse } from "next/server";

// export const GET = handleAuth();

export const GET = handleAuth({
  logout: async (req, res) => {
    return NextResponse.redirect(
      process.env.AUTH0_LOGOUT_OIDC +
        "?post_logout_redirect_uri=" +
        encodeURIComponent(process.env.AUTH0_BASE_URL)
    );
  },
});

```

This follows the info at [Use the OIDC Endpoint to Log Users Out of Auth0](https://auth0.com/docs/authenticate/login/logout/log-users-out-of-auth0), except:

- The final logged out screen has no way for the user to get back to the original site loging page
- The post\_logout\_redirect\_uri is ignored, despite being described as operational at [Use the OIDC Endpoint to Log Users Out of Auth0](https://auth0.com/docs/authenticate/login/logout/log-users-out-of-auth0#redirect-users-after-logout) and despite my adding it to both Tenant Settings, and my Application Settings - in the allowable Logout URL fields

My questions are:

1. How are others logging out users using the auth0-nextjs library?
2. Are others finding that login is not always requires despite calling logout?
3. Are others using this OIDC approach to logging out at the identity provider side and if so, does post\_logout\_redirect work?

---

<div class="post-metadata">

**Author:** ![MarkBoucher72](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/markboucher72/32/51825_2.png) [@MarkBoucher72](https://community.auth0.com/u/MarkBoucher72)\
**Post date:** [October 20, 2023, 4:42pm UTC](https://community.auth0.com/t/logging-out-completely/118940/2 "2023-10-20T16:42:12Z")

</div>

I’ve encountered similar logout issues. Many resort to OIDC logout, but it can be inconsistent. Post-logout redirect sometimes doesn’t work as expected. Looking for insights and best practices from others[.](https://demonslayeroutlet.com/collections/demon-slayer-pajamas/)

---

<div class="post-metadata">

**Author:** ![dalgonaio](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@dalgonaio](https://community.auth0.com/u/dalgonaio)\
**Post date:** [October 20, 2023, 9:32pm UTC](https://community.auth0.com/t/logging-out-completely/118940/3 "2023-10-20T21:32:23Z")

</div>

Hey all - for me, I got this fixed by going into my Dashboard \> Advanced \> Log In Session Management and changed it to nonpersistent and 1 minute each for the other values. Then waiting after logging out for 2 min before clicking login again.

---

<div class="post-metadata">

**Author:** ![allistair](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/allistair/32/49111_2.png) [@allistair](https://community.auth0.com/u/allistair)\
**Post date:** [October 23, 2023, 6:11pm UTC](https://community.auth0.com/t/logging-out-completely/118940/4 "2023-10-23T18:11:12Z")

</div>

Hey - that’s not the most usable solution though, as a non-persistent cookie will be removed on closing the browser, requiring a re-login if I close the tab/browser session.

Also, the cookie isn’t the issue here - as I mentioned, the cookie is removed with the /api/auth/logout call, the issue is that some other magic is logging the user back in without logging in if its within a time period of the logout.

For me, logout should mean logout, I don’t understand how this behaviour is useful or secure.

Hence needing this nuisance of needing to use the OIDC logout - suggesting something about the auth0-nextjs library is checking with Auth0 to see if the user is “logged in at the identity side” which needs to be something we can opt out of.

---

<div class="post-metadata">

**Author:** ![dalton](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dalton/32/48578_2.png) [@dalton](https://community.auth0.com/u/dalton)\
**Post date:** [November 9, 2023, 2:02am UTC](https://community.auth0.com/t/logging-out-completely/118940/5 "2023-11-09T02:02:07Z")

</div>

I figured out _a_ solution to this. Here’s what I did.

The reason this isn’t working is because the Auth0 NextJS handleLogout just deletes the cookie set on your AUTH0\_BASE\_URL, but it doesn’t delete the cookie on your AUTH0\_ISSUER\_BASE\_URL, so when you login _after_ logging out, it redirects to that issuer url, sees a valid auth cookie and signs you _right back in_.

What I found is Auth0 has a `/v2/logout` endpoint that deletes the cookie from the issuer url, so if you redirect to that endpoint after the handleLogout it will also remove the cookie from your AUTH0\_ISSUER\_BASE\_URL. Here’s my implementation.

```auto
/api/auth/[auth0]/route.ts

import { handleAuth, handleLogin, handleLogout } from '@auth0/nextjs-auth0';

const logoutUrl = [
  `${process.env.AUTH0_ISSUER_BASE_URL}/v2/logout?`,
  `client_id=${process.env.AUTH0_CLIENT_ID}`,
  `&returnTo=${process.env.AUTH0_BASE_URL}`,
];

export const GET = handleAuth({
  logout: handleLogout({ returnTo: logoutUrl.join('') }),
});

```

---

<div class="post-metadata">

**Author:** ![muhammad.awais.77831](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/muhammad.awais.77831/32/51517_2.png) [@muhammad.awais.77831](https://community.auth0.com/u/muhammad.awais.77831)\
**Post date:** [January 8, 2024, 10:10am UTC](https://community.auth0.com/t/logging-out-completely/118940/6 "2024-01-08T10:10:44Z")

</div>

Thanks it worked for me as well can you please share how you handle token in next js ?

---

<div class="post-metadata">

**Author:** ![dalton](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dalton/32/48578_2.png) [@dalton](https://community.auth0.com/u/dalton)\
**Post date:** [January 8, 2024, 2:47pm UTC](https://community.auth0.com/t/logging-out-completely/118940/7 "2024-01-08T14:47:47Z")

</div>

Hey Muhammad, can you be more specific?

---

<div class="post-metadata">

**Author:** ![lucas8](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lucas8/32/49896_2.png) [@lucas8](https://community.auth0.com/u/lucas8)\
**Post date:** [April 4, 2024, 3:21pm UTC](https://community.auth0.com/t/logging-out-completely/118940/9 "2024-04-04T15:21:52Z")

</div>

Hi Dalton,  
Sadly, it didn’t work for me. The appSession cookie keep being added when reloading the page. Even when I send the site to other people, they can see my account logged in.

I found the issue is only client side userinfo updated, the server side session never deleted so it keeps login or keep previous logged in user info in ssr page and api. But I dont know how to solve this

---

<div class="post-metadata">

**Author:** ![rakesh.bitling](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rakesh.bitling/32/58421_2.png) [@rakesh.bitling](https://community.auth0.com/u/rakesh.bitling)\
**Post date:** [July 27, 2024, 4:46am UTC](https://community.auth0.com/t/logging-out-completely/118940/10 "2024-07-27T04:46:42Z")

</div>

> [@dalton](#):
>
> Thanks it worked for me as well can you please share how you handle token in next js ?

import { handleLogout } from ‘@auth0/nextjs-auth0’;

export default async function logout(req, res) {  
try {  
await handleLogout(req, res, {  
returnTo: ‘[http://localhost:3000/login](http://localhost:3000/login)’  
});  
} catch (error) {  
console.error(error);  
res.status(error.status || 500).end(error.message);  
}  
}  
This code is not working at all please help me Sir please please

---

<div class="post-metadata">

**Author:** ![muhammad.awais.77831](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/muhammad.awais.77831/32/51517_2.png) [@muhammad.awais.77831](https://community.auth0.com/u/muhammad.awais.77831)\
**Post date:** [July 27, 2024, 7:43am UTC](https://community.auth0.com/t/logging-out-completely/118940/11 "2024-07-27T07:43:45Z")

</div>

This is working fine for me

```auto
// app/src/app/api/auth/[auth0]/route.ts

import { handleAuth, handleLogout } from '@auth0/nextjs-auth0'

export const GET = handleAuth({
  logout: handleLogout({ returnTo: 'http://localhost:3000/api/auth/login' }),
})

```

You need to define logout url which you pass in returnTo in Auth0 dashboard like `http://localhost:3000/api/auth/login`

---

<div class="post-metadata">

**Author:** ![rakesh.bitling](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rakesh.bitling/32/58421_2.png) [@rakesh.bitling](https://community.auth0.com/u/rakesh.bitling)\
**Post date:** [July 27, 2024, 8:16am UTC](https://community.auth0.com/t/logging-out-completely/118940/12 "2024-07-27T08:16:34Z")

</div>

when I wrote the same code it is redirecting to  
tenant\_auth\_domain/v2/logout?returnTo=[http://localhost:3000/api/auth/login&client\_id=1234](http://localhost:3000/api/auth/login&client_id=1234) as 404 URL same code i wrote friend.

---

<div class="post-metadata">

**Author:** ![t.salzmann](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/t.salzmann/32/59248_2.png) [@t.salzmann](https://community.auth0.com/u/t.salzmann)\
**Post date:** [August 16, 2024, 12:03pm UTC](https://community.auth0.com/t/logging-out-completely/118940/14 "2024-08-16T12:03:12Z")

</div>

I experienced the same problem: users can log out normally, but are directly logged in when clicking the login button without the Auth0 universal login. I believe I finally found another solution without having to call the `/v2/logout` endpoint, although I am not sure as to why it works and if this will also help others out there. In my NextJs project (with App Router) all the Auth0 calls (login, logout etc) were based on the `<Link>` component (`next/link`). Changing it to a native `<a>` tag worked for me without having to customize `/api/auth/[auth0]/route.ts`

Change

```auto
import Link from "next/link";

<Link href="/api/auth/logout">Logout</Link>

```

to

```auto
<a href="/api/auth/logout">Logout</a>

```

An alternative is doing a force push using the router, as highlighted [here](https://github.com/auth0/nextjs-auth0/issues/362#issuecomment-1773091912). For example, using a `<button>` component:

```auto
import { useRouter } from 'next/navigation';

export default function LogoutButton() {

   const router = useRouter();

   return <button onClick={() => router.push("/api/auth/logout")}>Logout</button>

}

```

---

<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:** [August 30, 2024, 12:03pm UTC](https://community.auth0.com/t/logging-out-completely/118940/15 "2024-08-30T12:03:24Z")

</div>

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