<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Tai Tran</title>
    <description>The latest articles on Forem by Tai Tran (@tai_tran_36c0d039fde1e560).</description>
    <link>https://forem.com/tai_tran_36c0d039fde1e560</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3695501%2F6a1a68a3-d2b2-41dd-821a-e5f724ba9e33.jpeg</url>
      <title>Forem: Tai Tran</title>
      <link>https://forem.com/tai_tran_36c0d039fde1e560</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tai_tran_36c0d039fde1e560"/>
    <language>en</language>
    <item>
      <title>Handling JWT Refresh Tokens in Axios without the Headache</title>
      <dc:creator>Tai Tran</dc:creator>
      <pubDate>Tue, 06 Jan 2026 05:31:10 +0000</pubDate>
      <link>https://forem.com/tai_tran_36c0d039fde1e560/handling-jwt-refresh-tokens-in-axios-without-the-headache-56nb</link>
      <guid>https://forem.com/tai_tran_36c0d039fde1e560/handling-jwt-refresh-tokens-in-axios-without-the-headache-56nb</guid>
      <description>&lt;p&gt;If you build Frontend applications with React, Vue, or Angular, you’ve probably faced this scenario:&lt;/p&gt;

&lt;p&gt;Your user's Access Token expires.&lt;/p&gt;

&lt;p&gt;The user loads a dashboard that fires 3 API requests simultaneously.&lt;/p&gt;

&lt;p&gt;All 3 requests fail with 401 Unauthorized.&lt;/p&gt;

&lt;p&gt;Your app tries to refresh the token... 3 times in a row. 💥&lt;/p&gt;

&lt;p&gt;The first refresh succeeds, but the second one invalidates the first one. The user gets logged out randomly.&lt;/p&gt;

&lt;p&gt;This is called the Race Condition.&lt;/p&gt;

&lt;p&gt;To fix this, you need a complex logic: A Promise Queue. You need to pause all failed requests, wait for one refresh to happen, and then retry them all with the new token.&lt;/p&gt;

&lt;p&gt;I got tired of copy-pasting this boilerplate code into every project, so I built a tiny, battle-tested library to handle it for me.&lt;/p&gt;

&lt;p&gt;Meet axios-auth-refresh-queue.&lt;/p&gt;

&lt;p&gt;Why use this instead of coding it yourself?&lt;br&gt;
⚡ Ultra-lightweight: It’s 641 Bytes (minified + gzipped). Yes, less than 1KB.&lt;/p&gt;

&lt;p&gt;🛡 Bulletproof: Handles race conditions, infinite loops, and failed refreshes gracefully.&lt;/p&gt;

&lt;p&gt;🐞 Debug Mode: Comes with a built-in logger to see exactly what's happening (Refreshing? Queuing? Retrying?).&lt;/p&gt;

&lt;p&gt;🟦 TypeScript: Fully typed out of the box.&lt;/p&gt;

&lt;p&gt;How to use it&lt;br&gt;
It takes less than 2 minutes to set up.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install&lt;br&gt;
&lt;code&gt;npm install axios-auth-refresh-queue&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Setup&lt;br&gt;
You just need two things: a function to refresh your token and the interceptor setup.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios';
import { applyAuthTokenInterceptor } from 'axios-auth-refresh-queue';

// 1. Create your Axios instance
const apiClient = axios.create({
  baseURL: 'https://api.example.com',
});

// 2. Define your Refresh Logic
// This function should return the new access token
const requestRefresh = async (refreshToken: string) =&amp;gt; {
  const response = await axios.post('/auth/refresh', { token: refreshToken });
  return {
    accessToken: response.data.accessToken,
    refreshToken: response.data.refreshToken,
  };
};

// 3. Apply the interceptor
applyAuthTokenInterceptor(apiClient, {
  requestRefresh,  // The async function to call backend
  debug: true,     // 🐞 Enable console logs to see the magic!

  onSuccess: (newTokens) =&amp;gt; {
    // Save new tokens to localStorage/Store
    localStorage.setItem('token', newTokens.accessToken);
  },

  onFailure: (error) =&amp;gt; {
    // Refresh failed? Log the user out
    console.error('Session expired', error);
    window.location.href = '/login';
  }
});

export default apiClient;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That's it! Now, whenever a 401 happens:&lt;/p&gt;

&lt;p&gt;The library pauses all other requests.&lt;/p&gt;

&lt;p&gt;It calls your requestRefresh function once.&lt;/p&gt;

&lt;p&gt;It updates the header and retries all original requests automatically.&lt;/p&gt;

&lt;p&gt;Cool Features&lt;br&gt;
🐞 Debug Mode&lt;br&gt;
Not sure if it's working? Just enable debug: true and check your console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Auth-Queue] 🚨 401 Detected from /api/user
[Auth-Queue] ⏳ Refresh already in progress. Adding to queue...
[Auth-Queue] ✅ Refresh Successful! Retrying queued requests.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⏩ Skip Auth&lt;br&gt;
Need to call a public API that might return 401 but shouldn't trigger a refresh?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;axios.get('/api/public-status', { 
    skipAuthRefresh: true 
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Give it a try!&lt;br&gt;
I built this to save time for myself and my team, and I hope it helps you too. It’s open-source, fully tested, and ready for production.&lt;/p&gt;

&lt;p&gt;📦 NPM: npmjs.com/package/axios-auth-refresh-queue&lt;br&gt;
🐙 GitHub: &lt;a href="https://github.com/Eden1711/axios-auth-refresh" rel="noopener noreferrer"&gt;https://github.com/Eden1711/axios-auth-refresh&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you find it useful, a ⭐️ on GitHub would mean the world to me!&lt;/p&gt;

&lt;p&gt;Happy coding! 💻&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
