<?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: kundan</title>
    <description>The latest articles on DEV Community by kundan (@fullstacklabs).</description>
    <link>https://dev.to/fullstacklabs</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%2F3976249%2Ff66d67a7-70ab-4b4f-8ceb-a9f0f2422893.png</url>
      <title>DEV Community: kundan</title>
      <link>https://dev.to/fullstacklabs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fullstacklabs"/>
    <language>en</language>
    <item>
      <title>Access token vs refresh token</title>
      <dc:creator>kundan</dc:creator>
      <pubDate>Tue, 09 Jun 2026 15:40:16 +0000</pubDate>
      <link>https://dev.to/fullstacklabs/access-token-vs-refresh-token-h4f</link>
      <guid>https://dev.to/fullstacklabs/access-token-vs-refresh-token-h4f</guid>
      <description>&lt;h2&gt;
  
  
  Refresh Token:-
&lt;/h2&gt;

&lt;p&gt;A Refresh Token is a long-lived token used to generate new access tokens without login the user again.&lt;/p&gt;

&lt;p&gt;Refresh tokens must be stored securely (server-side only)&lt;/p&gt;

&lt;p&gt;Once the Refresh token is expire user must login again (re-authentication need).&lt;/p&gt;

&lt;p&gt;Access tokens are short-lived token (15 minutes). When they expire, instead of making the user log in again, the app quietly uses the refresh token behind the scenes.&lt;/p&gt;

&lt;h2&gt;
  
  
  How refresh token works:-
&lt;/h2&gt;

&lt;p&gt;The user login and gets both an access token and a refresh token&lt;br&gt;
The app uses the access token to call APIs.&lt;br&gt;
After 15 min the access token expires the API returns a 401 Unauthorized error.&lt;br&gt;
The app sends the refresh token to the Authorization Server.&lt;br&gt;
The Authorization Server returns a fresh access token.&lt;br&gt;
The user never notices anything they stay logged in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analogy:-
&lt;/h2&gt;

&lt;p&gt;Your office keycard expires every day at midnight. Instead of going back to reception every morning, you have a special master card that automatically issues you a new keycard. The master card much longer, but is stored securely in HR (your server),not to our shirt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Access Token:-
&lt;/h2&gt;

&lt;p&gt;An Access Token is a short-lived(15 min) token that allows a user to access protected resources.&lt;/p&gt;

&lt;p&gt;Access Token are stored in the HttpOnly cookies.&lt;/p&gt;

&lt;p&gt;Used to access APIs and protected routes and send in every request.&lt;/p&gt;

&lt;p&gt;Access token use the refresh token to get new Access Token.&lt;/p&gt;

&lt;p&gt;If someone steals an access token, the damage is limited because it expires quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  :- Why Do We Need Both?
&lt;/h2&gt;

&lt;p&gt;Why not just use one token?&lt;/p&gt;

&lt;p&gt;If we use only a long-lived access token:&lt;/p&gt;

&lt;p&gt;The user stays logged in for a long time.&lt;br&gt;
But if the token is stolen, the attacker can use it for a long period.&lt;/p&gt;

&lt;p&gt;If we use only a short-lived access token:&lt;/p&gt;

&lt;p&gt;Security improves.&lt;/p&gt;

&lt;p&gt;But users would have to log in repeatedly.&lt;/p&gt;

&lt;p&gt;Using both tokens gives us the best of both worlds:&lt;/p&gt;

&lt;p&gt;Access Token → Better security.&lt;br&gt;
Refresh Token → Better user experience.&lt;/p&gt;

&lt;p&gt;This balance is why modern applications use both&lt;/p&gt;

&lt;h2&gt;
  
  
  flow:-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;User logs in
↓&lt;/li&gt;
&lt;li&gt;Backend verifies credentials
↓&lt;/li&gt;
&lt;li&gt;Backend generates:

&lt;ul&gt;
&lt;li&gt;Access Token&lt;/li&gt;
&lt;li&gt;Refresh Token
↓&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Backend sends both as HttpOnly cookies
↓&lt;/li&gt;
&lt;li&gt;Backend stores the refresh token hash in the database&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Later:-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Access Token expires
↓&lt;/li&gt;
&lt;li&gt;Browser sends Refresh Token cookie
↓&lt;/li&gt;
&lt;li&gt;Backend verifies:

&lt;ul&gt;
&lt;li&gt;Is the refresh token valid?&lt;/li&gt;
&lt;li&gt;Is it expired?&lt;/li&gt;
&lt;li&gt;Does its hash match the one in the DB?
↓&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If valid:

&lt;ul&gt;
&lt;li&gt;Generate a new Access Token&lt;/li&gt;
&lt;li&gt;(Often also generate a new Refresh Token)
↓&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Send new token back in HttpOnly cookies&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>api</category>
      <category>backend</category>
      <category>security</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
