Back to articles
AuthNode.jsSecurityJWT

Auth From Scratch: scrypt, Short-Lived JWTs and Rotating Refresh Tokens

DK

Deepak Kandpal

Full Stack Developer

September 26, 2026 8 min read
Auth From Scratch: scrypt, Short-Lived JWTs and Rotating Refresh Tokens

For my URL shortener Trim, I made a deliberate decision: no auth library. Not because libraries are bad — but because auth is one of those things every developer uses and few truly understand. Here's what building it from scratch taught me.

1. Hashing passwords with scrypt

Most tutorials reach for bcrypt. It's fine — but Node.js ships scrypt in the crypto module, and it's memory-hard by design, which makes GPU-based brute forcing much more expensive:

import { scrypt, randomBytes, timingSafeEqual } from "node:crypto";

function hashPassword(password: string): Promise<string> {
  return new Promise((resolve, reject) => {
    const salt = randomBytes(16).toString("hex");
    scrypt(password, salt, 64, (err, derived) => {
      if (err) reject(err);
      else resolve(salt + ":" + derived.toString("hex"));
    });
  });
}

Two details matter: a unique random salt per user (so rainbow tables are useless), and timingSafeEqual when comparing hashes (so attackers can't measure response times to guess the hash byte-by-byte).

2. Short-lived access tokens

The access JWT lives for 15 minutes. That's it. If a token leaks — via logs, a compromised tab, anything — its blast radius is a quarter of an hour, not a month. The JWT carries only the user id and nothing sensitive.

3. Rotating refresh tokens in HttpOnly cookies

This is the part most tutorials skip. The refresh token lives in an HttpOnly, Secure, SameSite cookie — JavaScript can never read it, which kills XSS theft. And it rotates: every time you use a refresh token, the server verifies it, deletes it, and issues a brand-new pair.

Why rotation? Theft detection. If an attacker steals a refresh token and uses it, the legitimate user's copy stops working — the server sees a reused token and can invalidate the whole session family. That reuse signal is something non-rotating tokens can never give you.

// refresh flow (simplified)
const stored = await db.refreshToken.findUnique({ where: { hash } });
if (!stored) throw new Error("reuse detected - kill all sessions");
await db.refreshToken.delete({ where: { hash } }); // one-time use
const next = createRefreshToken(userId);
await db.refreshToken.create({ data: { hash: sha256(next), userId } });
setCookie(res, "refresh", next, { httpOnly: true, secure: true, sameSite: "strict" });

Note the tokens are stored hashed (SHA-256) — a database leak shouldn't hand out live sessions.

What I'd do differently

For a client project with deadlines, I'd still reach for a battle-tested library. But building it once from scratch means I now read auth code with understanding instead of trust. That's worth a weekend.

Share this article
DK

Written by

Deepak Kandpal

Full Stack Developer building AI-assisted products, developer tools, and modern web experiences.

View portfolio