<?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: Sudhi Ranjan Gupta</title>
    <description>The latest articles on Forem by Sudhi Ranjan Gupta (@sudhi_ranjangupta_adc8df).</description>
    <link>https://forem.com/sudhi_ranjangupta_adc8df</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%2F1576120%2Fa64f3512-3e85-4062-8942-afb7ee4b067b.jpg</url>
      <title>Forem: Sudhi Ranjan Gupta</title>
      <link>https://forem.com/sudhi_ranjangupta_adc8df</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sudhi_ranjangupta_adc8df"/>
    <language>en</language>
    <item>
      <title>Session Management, Tokens &amp; Refresh Tokens</title>
      <dc:creator>Sudhi Ranjan Gupta</dc:creator>
      <pubDate>Sun, 27 Oct 2024 06:51:05 +0000</pubDate>
      <link>https://forem.com/sudhi_ranjangupta_adc8df/session-management-tokens-refresh-tokens-45m3</link>
      <guid>https://forem.com/sudhi_ranjangupta_adc8df/session-management-tokens-refresh-tokens-45m3</guid>
      <description>&lt;p&gt;The working cycle of &lt;strong&gt;session expiration, refresh token, and re-login&lt;/strong&gt; follows a common pattern in token-based authentication systems (like JWT), and it ensures secure access while balancing user experience. Here’s how each component typically fits into the cycle:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Session Expiration:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Session Expiry&lt;/strong&gt; occurs when the token or session reaches its validity period. A token (like JWT) generally has a &lt;strong&gt;short lifespan&lt;/strong&gt; to mitigate security risks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access Token&lt;/strong&gt;: This token is used to authenticate requests to the server. It is usually short-lived (e.g., 15-30 minutes) for security reasons.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mechanism&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;When the &lt;strong&gt;access token&lt;/strong&gt; expires, the client can no longer access protected resources using that token.&lt;/li&gt;
&lt;li&gt;At this point, the client needs to either &lt;strong&gt;refresh the token&lt;/strong&gt; using a refresh token or force the user to log in again.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Refresh Token:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;refresh token&lt;/strong&gt; is a long-lived token that allows the user to obtain a new access token without re-logging in. Its expiration period is usually longer (e.g., weeks or months) than the access token.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Working:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;When the &lt;strong&gt;access token expires&lt;/strong&gt;, the client (typically a front-end app) sends the &lt;strong&gt;refresh token&lt;/strong&gt; to the server in exchange for a new access token.&lt;/li&gt;
&lt;li&gt;The server checks the &lt;strong&gt;refresh token&lt;/strong&gt; to ensure it’s valid and hasn’t expired. If it’s valid, the server generates and returns a new access token to the client.&lt;/li&gt;
&lt;li&gt;This happens transparently to the user, meaning they can continue using the application without re-logging in.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Scenarios when Refresh Token works:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Refresh tokens are often stored securely (e.g., in HTTP-only cookies) and are not sent with every request—only when the access token expires.&lt;/li&gt;
&lt;li&gt;If the refresh token is valid, it grants a new access token without needing to authenticate again.&lt;/li&gt;
&lt;li&gt;If the refresh token is &lt;strong&gt;expired or invalid&lt;/strong&gt;, the user must re-login to generate new tokens.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Re-Login (When Refresh Token Expires):&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If the &lt;strong&gt;refresh token&lt;/strong&gt; also expires or becomes invalid (e.g., user logs out from all devices, or the refresh token is compromised), the user must &lt;strong&gt;re-authenticate&lt;/strong&gt; by logging in again.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scenarios when Re-Login is Needed:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The refresh token itself has expired, typically after a long period of inactivity (weeks or months).&lt;/li&gt;
&lt;li&gt;The user logs out manually, clearing both access and refresh tokens.&lt;/li&gt;
&lt;li&gt;The refresh token is &lt;strong&gt;revoked&lt;/strong&gt; on the server side, which can happen for security reasons (e.g., password change or account compromise).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Typical Working Cycle:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Initial Login:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The user logs in with credentials (username, password, or via an OAuth2 provider).&lt;/li&gt;
&lt;li&gt;The server issues both an &lt;strong&gt;access token&lt;/strong&gt; (short-lived) and a &lt;strong&gt;refresh token&lt;/strong&gt; (longer-lived).&lt;/li&gt;
&lt;li&gt;The access token is used to authenticate API requests, while the refresh token is stored securely (usually in a cookie or secure storage).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Session In-Progress (Using Access Token):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The client sends requests to the server using the access token for authentication.&lt;/li&gt;
&lt;li&gt;This continues until the &lt;strong&gt;access token expires&lt;/strong&gt; (e.g., after 15 minutes).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access Token Expiry:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;After the &lt;strong&gt;access token expires&lt;/strong&gt;, the client detects that the token is no longer valid (e.g., a 401 Unauthorised response from the server).&lt;/li&gt;
&lt;li&gt;The client then sends the &lt;strong&gt;refresh token&lt;/strong&gt; to the server to get a new access token.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refresh Token Flow:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If the refresh token is valid:

&lt;ul&gt;
&lt;li&gt;The server issues a new access token.&lt;/li&gt;
&lt;li&gt;The client continues using the new access token to access protected resources.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If the refresh token has &lt;strong&gt;expired or is invalid&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The server responds with an error (e.g., 403 Forbidden), indicating the client must log in again.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-Login:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If the refresh token is no longer valid (expired, revoked, etc.), the client will redirect the user to the login page.&lt;/li&gt;
&lt;li&gt;The user will need to log in again to obtain a new pair of access and refresh tokens.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  When to Use Each Component:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Access Token:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Used to authorise and authenticate most API requests.&lt;/li&gt;
&lt;li&gt;Works until it &lt;strong&gt;expires&lt;/strong&gt;, which is typically a short time (minutes).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refresh Token:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Used to get a new access token without re-logging in when the access token expires.&lt;/li&gt;
&lt;li&gt;Works until it &lt;strong&gt;expires&lt;/strong&gt; (usually a much longer time, weeks/months).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-Login:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Required when both the access and refresh tokens are &lt;strong&gt;expired&lt;/strong&gt; or &lt;strong&gt;revoked&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;User must provide credentials again.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Mermaid Version To understand the flow in depth:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sequenceDiagram
    participant User
    participant ClientApp
    participant AuthServer
    participant API

    Note over User,ClientApp: Initial Login
    User -&amp;gt;&amp;gt; ClientApp: Provide credentials (e.g., username, password)
    ClientApp -&amp;gt;&amp;gt; AuthServer: Send credentials
    AuthServer -&amp;gt;&amp;gt; ClientApp: Access Token (15 mins) &amp;amp; Refresh Token (30 days)
    ClientApp -&amp;gt;&amp;gt; User: Logged In, Tokens stored (Access Token &amp;amp; Refresh Token)

    Note over ClientApp,API: Session In-Progress (Using Access Token)
    ClientApp -&amp;gt;&amp;gt; API: Send Access Token
    API -&amp;gt;&amp;gt; ClientApp: Response (Success)

    Note over ClientApp,API: Access Token Expired (e.g., after 15 mins)
    ClientApp -&amp;gt;&amp;gt; API: Send Access Token (Expired)
    API -&amp;gt;&amp;gt; ClientApp: 401 Unauthorized (Access Token expired)

    Note over ClientApp,AuthServer: Refresh Token Flow (Client sends Refresh Token)
    ClientApp -&amp;gt;&amp;gt; AuthServer: Send Refresh Token
    alt Refresh Token Valid
        AuthServer -&amp;gt;&amp;gt; ClientApp: New Access Token
        ClientApp -&amp;gt;&amp;gt; API: Send New Access Token
        API -&amp;gt;&amp;gt; ClientApp: Response (Success)
    else Refresh Token Expired
        AuthServer -&amp;gt;&amp;gt; ClientApp: 403 Forbidden (Re-login required)
        ClientApp -&amp;gt;&amp;gt; User: Redirect to Login (Session Expired)
    end

    Note over User,ClientApp: Re-Login (Required)
    User -&amp;gt;&amp;gt; ClientApp: Provide credentials
    ClientApp -&amp;gt;&amp;gt; AuthServer: Send credentials
    AuthServer -&amp;gt;&amp;gt; ClientApp: New Access Token &amp;amp; Refresh Token
    ClientApp -&amp;gt;&amp;gt; User: Logged In, Tokens refreshed

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd41f1k90efzj215s8fc6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd41f1k90efzj215s8fc6.png" alt="Image description" width="800" height="799"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mathematical Example
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Login&lt;/strong&gt; → User logs in → Receives &lt;strong&gt;access token&lt;/strong&gt; (15 mins) + &lt;strong&gt;refresh token&lt;/strong&gt; (30 days).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access Token Expiry&lt;/strong&gt; → After 15 minutes, access token expires → Client sends refresh token to server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refresh Token Valid&lt;/strong&gt; → If refresh token is valid → Server issues a new access token → User continues without re-logging in.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refresh Token Expiry&lt;/strong&gt; → After 30 days (or on logout), refresh token expires → User must re-login to get a new set of tokens.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>blog</category>
      <category>webdev</category>
      <category>systemdesign</category>
      <category>security</category>
    </item>
  </channel>
</rss>
