Access Token and Refersh Token

Gunjan GidwaniGunjan Gidwani
3 min read

🔐 What is an Access Token?

  • Purpose: It’s the short-lived credential you present to access protected APIs.

  • Lifespan: Typically expires in minutes or hours.

  • Analogy: Think of it as a temporary pass that lets you in—but once it’s expired, you’re locked out.

  • Why short-lived? Limits risk if stolen—once expired, it’s useless.


🕶️ What is a Refresh Token?

  • Purpose: This long-lived token allows you to seamlessly obtain a new access token.

  • Lifespan: Can last days to months.

  • Analogy: It's like a passport—when your pass (access token) expires, you present your passport (refresh token) to get a new pass.

  • Why use it? Keeps you logged in without re-entering your password and avoids constant short-lived login loops.


🔄 How They Work Together

  1. Initial login → Server issues both tokens.

  2. Client uses the access token to call APIs.

  3. Upon expiration, the client silently sends the refresh token to the Auth Server.

  4. Auth back-end validates it, then returns a new access token—and often rotates the refresh token too.

  5. The client stores these securely and continues operations seamlessly.


🛡️ Security Best Practices

  • Rotate refresh tokens: Best practice is to issue a new refresh token each time one is used, invalidating the old one—reducing the damage if stolen.

  • Reuse detection: If a refresh token is reused, immediately revoke that token chain to protect the user.

  • Secure storage:

    • Access tokens can live in memory or secure HTTP-only cookies (minimizes XSS).

    • Refresh tokens must be stored more securely—preferably server-side or in secure device keychains.


🧭 Why Not Use Just One Token?

  • Access tokens alone must either be long-lived (bad for security) or require constant re-login (bad for user experience).

  • Refresh tokens enable:

    • Short-lived access tokens (safer),

    • Seamless long sessions,

    • Controlled session invalidation.

This balanced flow mitigates risk while preserving usability.


🧩 In Summary

Token TypeLifespanUse CaseSecurity Consideration
Access TokenShort-livedAccess protected resources (APIs)Clean exposure window; careless if stolen
Refresh TokenLong-livedRenew access token without re-loginMust be stored securely; rotated/revoked
  • Access tokens are your everyday pass.

  • Refresh tokens are the backup that keeps your session alive.

  • The duo work together to balance security and user experience—reducing the risk of token theft while avoiding annoying logouts.


If you're building APIs or web/mobile apps, implementing this two-token system with rotation and secure storage is considered the modern best practice.

Also if you want more info for Authorization in Node.js please watch https://www.youtube.com/watch?v=7DVpag3cO0g&t=13s
This is very helpful series on Node.js

0
Subscribe to my newsletter

Read articles from Gunjan Gidwani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gunjan Gidwani
Gunjan Gidwani