Skip to content

ChezFre/ky-with-token-refresh

Repository files navigation

ky with JWT interceptors

This project uses ky with @tanstack/query to simulate a custom refresh logic for a jwt refresh flow when backend returns 401 Unauthorized.

The backend is mocked via msw and returns a status code 401 as long as the Authorization header does not contain Bearer valid-token" (see /src/mocks/handlers.ts)

Flow

  1. A request to the BE is made with an invalid token
  2. The backend responds with a 401 status code
  3. A request to /auth/refresh is made. All requests to the backend are blocked until /auth/refresh returns a response (always valid-token)
  4. The new token is stored
  5. The original request that responded with a 401 is retried
  6. The other request that was blocked will automatically continue with the new valid-token

The button Retry invalid flow removes the token and clears out the query cache.

When retying the invalid flow the second request will also go out to the backend and return a 401, this is because the second request starts before the backend responded to the first request, which can be verified from the waterfall in the network tab.

About

Experiment to create an auth refresh flow in ky when backend returns a 401

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors