This guide is written for developers who want a practical answer, not a giant theory dump. The goal is to help you understand the decision, use the idea in a real project, and explain it clearly in an interview or code review.
Quick answer
Use sessions when you want simple server-controlled login. Use JWTs when you truly need stateless tokens across services or clients.
Why developers search this
Auth choices are high-intent because developers are deciding how to build login.
It is a good SEO topic because the search usually happens near a real task: fixing a broken build, choosing an architecture pattern, deploying an app, reviewing AI-generated code, or preparing portfolio proof. Those searches are more valuable than broad “what is programming?” traffic because the reader needs an answer they can use today.
Mental model
The key question is not which one is modern. The key question is where you want control, how you revoke access, and who needs to read the token.
| Option | Best fit |
|---|---|
| Server session | Traditional web apps, easy revocation, central control |
| JWT access token | APIs, mobile clients, short-lived access |
| Refresh token | Longer login with careful rotation and storage |
Practical example
Browser login:
1. User signs in
2. Server creates session or tokens
3. Client sends cookie or authorization header
4. Server verifies on each request
5. Logout must invalidate future access
This example is intentionally small. In a real codebase, the surrounding details matter: naming, error handling, tests, runtime config, permissions, and how easy the next developer can understand the change.
Implementation checklist
- Prefer secure, httpOnly cookies for browser apps.
- Keep access tokens short-lived if using JWTs.
- Plan logout and revocation before launch.
- Protect refresh tokens carefully.
- Do not store sensitive secrets inside JWT payloads.
Common mistakes
- Putting private data in a JWT because it is base64 encoded.
- Using long-lived access tokens.
- Ignoring CSRF when using cookies.
- Choosing JWT only because tutorials use it.
- Forgetting password reset should invalidate old sessions.
How to explain this in an interview
Use a concrete sentence:
I used this pattern because [problem]. The main tradeoff was [tradeoff]. I verified it by [test or check].
That structure works because it shows judgment. Anyone can name a tool. Strong developers explain why they chose it, what could go wrong, and how they checked the result.
Related guides
- set up passkeys without losing access
- passkeys vs authenticator apps safer
- api error response format nodejs
Sources checked
Final takeaway
Use sessions when you want simple server-controlled login. Use JWTs when you truly need stateless tokens across services or clients. Keep the implementation small, verify the edge cases, and write the decision down so the next person can trust it.
Discussion
What would you try, change, or challenge after reading this guide? Specific results and errors help the next reader.
Comments will load as you reach this section.