What would it be the best way to clear the NextAuth.js session when trying to hit the backend (Apollo GraphQL) and it returns a 401 because the token has expired or is invalid?
I thought about an errorLink
and signout
, but as far as I know signout
cannot be used server side at getServerSideProps
, but only client-side.
What is the recommended way to do so? Is there any other way to implement a middleware to take care of that scenario?
Thanks
NextAuth. js by default uses JSON Web Tokens for saving the user's session. However, if you use a database adapter, the database will be used to persist the user's session. You can force the usage of JWT when using a database through the configuration options.
Luckily, Apollo provides a nice way for authenticating all requests by using the concept of middleware, implemented as an Apollo Link. import { setContext } from '@apollo/client/link/context'; This middleware will be invoked every time ApolloClient sends a request to the server.
signOut() clears the session by clearing the state in client side , here what you can do is to check from backend if the state exists and if it is not then do something instead of 401(Unauthorized). Hope You Read It: https://next-auth.js.org/getting-started/client#signout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With