About accessToken and refreshToken and difference between them

Sajju AhmadSajju Ahmad
3 min read

πŸ” What is an Access Token?

  • A small piece of data (usually a JWT) that proves the user is logged in.

  • It is sent with every request to protected routes (like fetching user profile, updating data, etc.).

  • The server checks the access token to see if the request is authorized.

  • Expires quickly (usually 15 mins to 1 hour) for security.

πŸ”„ Think of it like a visitor pass to a building β€” it gets you in, but expires quickly.

πŸ”‘ 1. Access Token

FeatureDescription
PurposeGrants access to protected resources (APIs, routes, data).
Short-livedUsually expires in 15 minutes to 1 hour for security.
Stored inUsually in HTTP-only cookies or memory/localStorage.
Used inSent with every request to authenticate the user.
PayloadTypically includes user info (like userId, email, role)
Security RiskIf stolen, attacker can act as the user β€” that's why it expires quickly.

πŸ” What is a Refresh Token?

  • A longer-lasting token that stays with the user even after the access token expires.

  • It is used to get a new access token without making the user log in again.

  • Stored securely (usually in an HTTP-only cookie or database).

  • Expires in days or weeks.

πŸ”„ Think of it like a master key stored securely β€” used to generate new visitor passes.

πŸ”„ 2. Refresh Token

FeatureDescription
PurposeUsed to generate a new access token without requiring user to log in again.
Long-livedUsually expires in 7 to 30 days.
Stored inSecurely stored in HTTP-only cookies or database.
Used inSent only once, when access token is expired, to get a new one.
PayloadOften only includes userId or minimal info (safer).
Security RiskIf leaked, can be used to regenerate tokens β€” must be stored & handled very securely.

❓Are both accessToken and refreshToken required?

Short answer:

  • βœ… No, only the accessToken is technically required for authentication to work.

  • πŸš€ But using both is strongly recommended for security and user experience.


βœ… When is only accessToken enough?

You can use just an accessToken if:

  • You don’t care if the user is forced to log in again every time the token expires.

  • You keep the token lifetime very short (e.g., 15 minutes).

  • Example: small internal apps or temporary logins.

But once the token expires, the user must log in again. This is annoying for users.


βœ… When should you use both accessToken and refreshToken?

Use both when:

  • You want a better user experience (auto-renew tokens behind the scenes).

  • You want extra security β€” refresh tokens can be revoked or rotated.

  • You're building a real-world app (e.g., e-commerce, social media, etc.).

When accessToken expires, the refreshToken helps generate a new one without logging in again.


βš–οΈ Summary

ScenarioUse only accessTokenUse both accessToken + refreshToken
Quick demo or test appβœ… OK❌ Overkill
Real-world app (login flow)❌ Bad UXβœ… Best practice
API without sessionsβœ… OK❌ Optional
Persistent sessions (web/mobile)βŒβœ… Required for smooth experience

πŸ” Pro Tip

Access Tokens = Short lifespan = Low risk
Refresh Tokens = Long lifespan = Store securely!

That’s it for this post!

Thanks for sticking around. If you have any questions or suggestions, drop a comment β€” I'd love to hear from you.

See you in the next article!

1
Subscribe to my newsletter

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

Written by

Sajju Ahmad
Sajju Ahmad