Lesson 52/100

Tutorials MEAN Stack Tutorial

Refresh Tokens — Complete Guide

Refresh Tokens — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MEAN Stack Tutorial on Toolliyo Academy.

On this page

MEAN Stack Tutorial · Lesson 52 of 100

Refresh Tokens

Stack ✓Projects

Projects · 2 — Apps · ~6 min · MEAN — Authentication & Security

What is this?

Refresh tokens are long-lived credentials stored securely (httpOnly cookie) used to obtain new access JWTs without re-login in MeanVerse apps.

Why should you care?

Short access tokens limit damage; refresh flow keeps UX smooth for 8-hour banking shifts.

See it live — copy this example

Paste into your MeanVerse project (Angular + Express + MongoDB), then run with ng serve / node / mongosh as noted.

router.post('/auth/refresh', async (req, res) => {
  const token = req.cookies.refreshToken;
  if (!token) return res.status(401).end();
  const stored = await RefreshToken.findOne({ tokenHash: hash(token), revoked: false });
  if (!stored || stored.expiresAt < new Date()) return res.status(401).end();
  const accessToken = signAccessToken({ id: stored.userId, roles: stored.roles });
  res.json({ accessToken });
});

res.cookie('refreshToken', rawToken, {
  httpOnly: true, secure: true, sameSite: 'strict', maxAge: 7 * 864e5
});

What happened?

  • Store hash of refresh token in MongoDB — leak of DB does not reveal usable tokens.
  • httpOnly cookie blocks JavaScript theft via XSS.

Practice next

  1. Issue refresh token on login; store hash in RefreshToken collection.
  2. Keep access token in memory or sessionStorage only.
  3. Rotate refresh token on each use — invalidate old hash.
  4. Detect refresh token reuse and revoke all user sessions.
  5. Add device name column for session management UI.

Remember

Access JWT short; refresh token long. httpOnly cookie + server-side hash storage. Rotation reduces replay attack value.

Teller workstation

Access expires every 15m; teller stays logged in all day via refresh.

Outcome: Stolen access token useless after expiry; refresh revocable centrally.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain JavaScript in the context of MEAN Stack.
Short answer: JavaScript runs single-threaded with an event loop. Closures capture lexical scope; promises/async handle I/O without blocking the UI thread. Real-world example (ShopNest) On the ShopNest storefront UI, thi…
Mid Detailed
What are common mistakes teams make with Components when using MEAN Stack?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Component…
Senior Detailed
How would you debug a production issue related to State in a MEAN Stack application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define State in…
Junior Detailed
Describe a real-world scenario where Performance mattered in a MEAN Stack project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Performan…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

MEAN Stack Tutorial
Course syllabus

MEAN Tutorial

MEAN — Stack Foundations
MEAN — Fundamentals
MEAN — TypeScript & RxJS
MEAN — Node.js & Express
MEAN — & Databases
MEAN — Authentication & Security
MEAN — Real-Time & Advanced Systems
MEAN — Performance & Testing
MEAN — DevOps & Deployment
MEAN — Enterprise Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details