<?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>DEV Community: Davies</title>
    <description>The latest articles on DEV Community by Davies (@kuntadev).</description>
    <link>https://dev.to/kuntadev</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%2F675646%2Fbdba0f00-e919-4f45-955e-b7d55320383a.jpg</url>
      <title>DEV Community: Davies</title>
      <link>https://dev.to/kuntadev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kuntadev"/>
    <language>en</language>
    <item>
      <title>How to use Axios interceptors to handle 401 API errors and refresh tokens in typescript.</title>
      <dc:creator>Davies</dc:creator>
      <pubDate>Tue, 13 Dec 2022 14:06:21 +0000</pubDate>
      <link>https://dev.to/kuntadev/how-to-use-axios-interceptors-to-handle-401-api-errors-and-refresh-tokens-in-typescript-2cjo</link>
      <guid>https://dev.to/kuntadev/how-to-use-axios-interceptors-to-handle-401-api-errors-and-refresh-tokens-in-typescript-2cjo</guid>
      <description>&lt;p&gt;To use &lt;a href="https://axios-http.com/docs/intro" rel="noopener noreferrer"&gt;Axios&lt;/a&gt; interceptors to handle &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401" rel="noopener noreferrer"&gt;401 API errors&lt;/a&gt; and refresh access/JSON web tokens(JWTs) tokens in TypeScript/Javascript, you can do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import the &lt;code&gt;axios&lt;/code&gt; library and create an instance of the &lt;code&gt;axios&lt;/code&gt; object:
&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';

const instance = axios.create();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Define a function to refresh the access token using a refresh token:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function refreshAccessToken(refreshToken: string): Promise&amp;lt;string&amp;gt; {
  // Send a request to the server to refresh the access token using the refresh token
  return axios
    .post('/refresh-token', { refreshToken })
    .then((response) =&amp;gt; response.data.accessToken);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add an interceptor to the &lt;code&gt;instance&lt;/code&gt; object to handle 401 errors. In the interceptor, check for a 401 error, and if one is found, refresh the access token using the refresh token and retry the original request:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;instance.interceptors.response.use(
  (response) =&amp;gt; response,
  (error) =&amp;gt; {
    const originalRequest = error.config;

    if (error.response.status === 401 &amp;amp;&amp;amp; !originalRequest._retry) {
      originalRequest._retry = true;
      const refreshToken = // Get the refresh token from storage

      return refreshAccessToken(refreshToken).then((accessToken) =&amp;gt; {
        // Replace the access token in the original request config
        originalRequest.headers['Authorization'] = `Bearer ${accessToken}`;
        return instance(originalRequest);
      });
    }

    return Promise.reject(error);
  }
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this in place, whenever an Axios request returns a 401 error, the interceptor will automatically refresh the access token and retry the original request.&lt;/p&gt;

&lt;p&gt;It's important to note, however, that this example is just a rough outline of how to use Axios interceptors to handle 401 errors and refresh access tokens in TypeScript. You will need to handle additional edge cases in a real-world application and implement proper error handling.&lt;/p&gt;

&lt;p&gt;For more information, be sure to refer to &lt;a href="https://axios-http.com/docs/interceptors" rel="noopener noreferrer"&gt;Axios' official documentation&lt;/a&gt; on interceptors.&lt;/p&gt;

</description>
      <category>webscraping</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
