Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NextAuth.js signout if Apollo GraphQL token is invalid or it has expired

Tags:

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

like image 307
VanPersie Avatar asked Nov 22 '20 11:11

VanPersie


People also ask

How does NextAuth JS work?

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.

How are HTTP requests sent ApolloClient authenticated?

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.


1 Answers

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

like image 157
Yog Sharma Avatar answered Sep 30 '22 18:09

Yog Sharma