Replies: 1 comment
-
have you found an answer to this? @chrber04 also how do you handle the case of the refresh token expiring? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I was thinking about switching to useSWR in my Nextjs project from just plain Axios, but I stumbled upon an issue with the authentication of my app. It uses JWT authentication where the refresh token is stored as a httpOnly cookie on the client, and the access token is stored in memory on the client.
Basically:
when a request to an endpoint which requires authentication returns a 403 status, a response interceptor that has been applied to the axios instance attempts to refresh the users tokens using the refresh token. It also applies a new header to the request to imply it has already been sent, as to prevent an infinite loop if the server again returns a 403 status. If the server returns with the new access token, then we append that to the Authorization header on the original request and retry it.
This is the hook I use for this logic:
Now my question is how would you go about implementing this using SWR? Would it be valid to just do like this?
const { data, error, isLoading } = useSWR("/api/users", usePrivateAxios());
It doesn't seem to complain or anything, but is it weird to be passing in the custom hook like this? And how will useSWR behave if the refresh fails? Anything else I didn't think about?
Beta Was this translation helpful? Give feedback.
All reactions