How often should tokens be refreshed for best practice?

NottDev
2 min readJan 12, 2025

--

The frequency with which you refresh tokens depends on the type of token you’re using (access token vs. refresh token) and your application’s security, scalability, and user experience requirements. Below are general best practices for token refresh:

1. Access Tokens

Access tokens are typically short-lived for security reasons. The recommended expiration time varies based on your application:

  • Short Expiry (15 minutes to 1 hour):
    -
    Common for highly sensitive applications (e.g., banking or healthcare).
    - Ensures stolen tokens cannot be used for long periods.
  • Longer Expiry (up to 24 hours):
    -
    Suitable for less sensitive applications (e.g., blogs or dashboards).
    - Balances security with user experience to minimize frequent refreshes.

Best Practice:

  • Set the expiration time to the shortest duration that balances security and user convenience.
  • Use a refresh token to obtain new access tokens without requiring the user to reauthenticate.

2. Refresh Tokens

Refresh tokens are long-lived and should be used sparingly to retrieve new access tokens. Their refresh policy depends on the use case:

  • Revocation and Rotation:
    -
    Refresh tokens should be revoked or rotated on every use to mitigate risks if they are leaked.
    - Ensure the new refresh token is securely stored.
  • Expiry:
    -
    Set refresh tokens to expire after a reasonable period (e.g., weeks to months) based on how often users need to log in.
    - Requiring periodic reauthentication ensures continued security.

Best Practice:

  • Refresh tokens should be refreshed only when needed (e.g., when the access token expires).
  • Implement idle session timeouts to revoke refresh tokens for inactive users.

3. Sliding Expiration

Some systems use sliding expiration for refresh tokens:

  • Refresh tokens expire after a fixed time (e.g., 30 days).
  • Every successful use of a refresh token resets its expiration time.
  • Mitigates risk by ensuring tokens are valid only for active sessions.

4. Token Refresh Triggers

Tokens should be refreshed:

  1. Before Access Token Expiry:
    -
    Use a buffer time (e.g., refresh 5 minutes before expiration) to avoid disruptions.
  2. On Token Use Error:
    -
    If the API responds with a 401 Unauthorized or similar error, attempt to refresh the token immediately.

5. Security Considerations

  • Secure Storage: Use secure methods (e.g., HTTP-only cookies or secure storage) to store tokens.
  • Transport Security: Always use HTTPS to prevent token interception.
  • Revoke Compromised Tokens: Implement mechanisms to invalidate tokens (e.g., revocation lists or JWT blacklisting).
  • Limit Scope: Use separate tokens for different purposes to minimize exposure of sensitive operations.

Example Timeline

  • Access Token Expiry: 30 minutes.
  • Refresh Token Expiry: 30 days with rotation.
  • Sliding Expiration: Reset refresh token expiry on each valid use.

By following these practices, you ensure tokens are refreshed as needed without overburdening your authentication service while maintaining strong security.

--

--

NottDev
NottDev

Written by NottDev

Your only limit is your mind.

No responses yet