NextAuth Programmatically SignOut on server-side
Asked Answered
P

1

10

How would I programmatically sign out a user in Next-Auth on the server side?

Inside /api/auth/[...nextauth].js there is a jwt function:

const callbacks = {    
 async jwt ({ token, user }) {
   
  // How to sign out a user?
  signOut();  <--- can't use this on the server side
 
  }
 }

Unfortunately, the signOut() function is only for the client side. It produces an error if used here.

I have read the very bare-bones Next Auth REST API page and I can't get a POST call to "/api/auth/signout" to work. If that is the correct method can someone show me some working code? Does it need any kind of authorization token?

https://next-auth.js.org/getting-started/rest-api

Thanks!

Permeance answered 2/5, 2023 at 12:48 Comment(0)
T
1

As of the time of this answer, there's seems to be no support for server-side logout on next-auth.

Source: nextauth Github issue 5334

There are workarounds like clearing the cookie or redirecting the user to an auto logout page, but nothing official or straight forward.

Thoroughbred answered 16/4 at 22:8 Comment(1)
I think the work around is to pass a bool back to the frontend, you check it in a useEffect hook and if it is true then run signOut(). Horrible to do and also fairly complex for a beginner, next-auth really should "just do" this.Permeance

© 2022 - 2024 — McMap. All rights reserved.