Skip to content
Backend & APIChapter 28 / 57Advanced7 min read

Authentication Patterns

Authentication best practices for sessions and tokens. Resolve the user in one place, fail closed, store only hashed tokens and use secure cookies.

On this page

1. What is this practice?

Authentication answers "who are you?". It is different from authorization, which asks "what are you allowed to do?".

There are three common and sound models. Pick one at a time, and do not invent a fourth yourself:

ModelWhen
Managed auth provider (Supabase Auth, Auth.js, etc.)A web app where users have accounts
Passwordless session (a code or link by email)Visitors or a small admin team with no classic account
Access tokens (with expiry)An API used by a mobile app or other clients

2. Why does it matter?

A real and very common bug: auth middleware wrapped in a try/catch. An error happened while verifying the token, the catch logged it and called next(). Result: a small database problem made every route public. This is called failing open.

Another: in Next.js, security was placed only in middleware. In 2025 a vulnerability appeared where sending a special header made the middleware get skipped entirely. Apps whose pages checked the user themselves were safe.

A third: session tokens were stored in plain text in the database. A backup leaked and every user's session was stolen.

Scattered authOne place, fail closed
Each route reads the cookie itselfOne getCurrentUser function, nobody else touches the cookie
On error the request continuesOn error, 401, never allow
Only middleware is the security boundaryEvery page and handler checks for itself
Token stored in plain text in the databaseOnly the SHA-256 hash is stored
Token from Math.random32 random bytes from crypto
Login reveals whether an email is registeredThe answer is the same in every case

3. When to follow it

LevelHow importantWhy
Learning projectLearn to use a providerWriting auth yourself is fine for learning, but not in production
Production codeRequired, and with a proven libraryA mistake here is a direct account takeover
Senior levelChoosing the model, fail closed reviewDesigning for outages, token rotation and machine callers

When to learn it: the first time you build a page for logged in users only. Look at Cookies & Headers and Authentication first.

4. Bad vs good code

Bad: fail open and a weak token

ts
export async function authMiddleware(request: Request, next: () => Promise<Response>) {
  try {
    const token = request.headers.get("authorization");
    request.user = await verifyToken(token);
  } catch (error) {
    console.log("auth failed", error);
  }
  return next();
}

export function createSessionToken() {
  return Math.random().toString(36).slice(2);
}

next() runs even on an error. Math.random is predictable and the token is not cryptographically secure.

Good: sessions in one module

File lib/auth/session.ts:

ts
import "server-only";

import { createHash, randomBytes } from "node:crypto";
import { cookies } from "next/headers";

import { insertSession, findActiveSessionByHash } from "./session.queries";
import { SESSION_COOKIE, SESSION_MAX_AGE_SECONDS } from "./session.types";

function hashToken(token: string) {
  return createHash("sha256").update(token).digest("hex");
}

export async function startSession(userId: string) {
  const token = randomBytes(32).toString("base64url");
  const expiresAt = new Date(Date.now() + SESSION_MAX_AGE_SECONDS * 1000);

  await insertSession({ userId, tokenHash: hashToken(token), expiresAt });

  const cookieStore = await cookies();
  cookieStore.set(SESSION_COOKIE, token, {
    httpOnly: true,
    secure: process.env.NODE_ENV === "production",
    sameSite: "lax",
    path: "/",
    maxAge: SESSION_MAX_AGE_SECONDS,
  });
}

export async function getCurrentUser() {
  const cookieStore = await cookies();
  const token = cookieStore.get(SESSION_COOKIE)?.value;
  if (!token) {
    return null;
  }

  const session = await findActiveSessionByHash(hashToken(token));
  if (!session) {
    return null;
  }

  return session.user;
}

findActiveSessionByHash only returns a row whose expires_at is in the future and whose revoked_at is empty. Logout revokes the row, so even a stolen token stops working.

For machine callers, such as cron, check a shared secret:

ts
import { timingSafeEqual } from "node:crypto";

export function isCronRequest(request: Request) {
  const secret = process.env.CRON_SECRET;
  if (!secret) {
    throw new MissingConfigError("CRON_SECRET");
  }

  const expected = Buffer.from(`Bearer ${secret}`);
  const received = Buffer.from(request.headers.get("authorization") ?? "");
  if (expected.length !== received.length) {
    return false;
  }

  return timingSafeEqual(expected, received);
}

If the secret is not set, the request is not allowed, and it becomes a 503 error.

What is the difference?

ThingBad codeGood code
On errornext(), everything publicnull or 401, nothing allowed
TokenMath.randomrandomBytes(32)
StoragePlain tokenSHA-256 hash
CookieDefault settingshttpOnly, secure, sameSite, maxAge
RevokeNot possibleThrough revoked_at
Secret compare!==timingSafeEqual

5. How to apply it

The straight path for auth
Pick one model and use a proven library
One function resolves the caller, nobody else reads the cookie
Fail closed on an error or a missing secret
Tokens from random bytes, store only the hash
Every page and handler calls the function itself

A few more important points:

  • For passwords use scrypt, argon2 or bcrypt, through a library. Never plain text or md5.
  • The response for login and "forgot password" should be the same whether or not the email is registered.
  • Rate limit the login endpoint.
  • proxy.ts (formerly middleware) should only refresh the session. It is a convenience, not a security boundary.
  • If the auth provider is down, keep "signed out" and "could not verify" as different states. Do not quietly treat an error as an anonymous user.
  • On mobile, keep the token in secure storage, not in ordinary storage.

6. Exceptions and common mistakes

Some things are not authentication but are fine: a long random link made for one job, such as confirming a booking. The conditions are that it expires, it is stored hashed if it grants writes, and it is noindex for search engines.

Mistake: Treating a client side flag as auth

Why? Anyone can write isAdmin true into localStorage, or call setIsAdmin on the page, from the browser console.

Better: Role and verification should come from the server on every request, and client flags should only be UI hints.

Mistake: Making middleware the only door

Why? One matcher mistake or a framework bug can skip the whole middleware and leave pages open.

Better: Every page, server action and route handler calls its own getCurrentUser.

Mistake: An auth bypass for tests that also works in production

Why? Forgetting one env variable opens the bypass in production, and anyone can use anyone's account.

Better: Log in for real with a seeded test user, and if a bypass exists, make the middleware itself refuse it in production.

Mistake: Inventing your own crypto or session system

Why? Details like timing attacks, token rotation and expiry are often missed.

Better: Use a proven provider or library and only learn its configuration.

7. What to learn before and after

Final quick revision

CheckQuestion
One placeDoes only one function resolve the caller?
Fail closedDoes the request stop on an error or a missing secret?
TokensAre they made from random bytes and stored as hashes?
CookieAre httpOnly, secure, sameSite and maxAge set?
Check everywhereDo pages and handlers check for themselves, not just the proxy?
EnumerationDoes the login response hide whether an account exists?