(Note · · 5 min read)

Your auth library does not run at the edge

Checking whether a request is signed in belongs in middleware, before a page renders. But middleware runs in a cut-down runtime: no database driver, no Node built-ins, no long-lived connections. An authentication approach that assumes a database lookup per request cannot run there at all.

By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka

The constraint is real, not a misconfiguration

Edge runtimes deliberately exclude most of Node's surface. Attempting to import a database client or a Node crypto module fails at build or at run time, and no amount of configuration changes it.

What is available is the web platform: fetch, the Web Crypto API, standard streams. Any session scheme that works there has to be expressible in those.

Put the claim in the cookie and sign it

Instead of a session identifier requiring a lookup, store the small facts a request needs — user, role or permission set, expiry — in the cookie, with a signature computed over them using a secret.

Middleware then verifies the signature with Web Crypto and makes a decision with no database access. That is the whole reason the approach fits: it is self-contained. The ERP I built signs its sessions with keyed hashing precisely so that sign-in works in the edge runtime.

Sign, do not encrypt, unless the contents are sensitive — and then do both. A signed cookie is readable by its holder, so it must contain nothing you would not show them.

Verify in constant time, and only what you trust

Compare signatures with a timing-safe comparison. Decode the payload only after the signature verifies — parsing first and checking later means acting on attacker-controlled data.

Never accept the algorithm or key identifier from inside the token as an instruction. The verifier decides which algorithm and key it will accept; that is a well-worn class of vulnerability in token verification.

The cost: you cannot revoke a signed token

This is the real trade-off. A valid signed cookie stays valid until it expires, so signing someone out, changing their permissions or disabling an account does not take effect immediately in middleware.

  • Keep expiry short, with a refresh that does check the database.
  • Keep a revocation list for the exceptional cases, checked where you can reach storage.
  • Re-check permissions on the server for any sensitive action rather than trusting the cookie's copy.
  • Include a counter in the token that increments when credentials change, so old tokens are detectable.

In practice middleware should be a cheap gate — is there a plausible session, redirect if not — while the authoritative check happens where the data is. Treating the edge decision as final is what turns this trade-off into a vulnerability.

Cookie attributes carry much of the security

Mark it http-only so scripts cannot read it, secure so it only travels over an encrypted connection, and same-site to limit cross-site submission. Scope the path, and set an explicit expiry rather than relying on the session ending.

Keep the token small. Cookies travel on every request including static assets, and a large session cookie is a tax on the whole site.

Plan for rotating the secret

Support more than one key at verification time — current plus previous — so the signing secret can be rotated without signing everybody out. A scheme with one hardcoded key is a scheme that will never be rotated, including after the key has leaked.

Common questions

Why does my auth library fail in Next.js middleware?

Because the edge runtime deliberately excludes most of Node's surface — no database driver, no Node crypto, no long-lived connections. Any session scheme there must be expressible with fetch and the Web Crypto API.

How do sessions work without a database lookup?

Put the small facts a request needs — user, permissions, expiry — in the cookie and sign them with a secret. Middleware verifies the signature with Web Crypto and decides with no database access, which is what makes the approach self-contained.

What is the downside of signed session cookies?

You cannot revoke one. It stays valid until expiry, so signing out, changing permissions or disabling an account does not take effect immediately. Keep expiry short, re-check permissions server-side for sensitive actions, and keep a revocation list for exceptions.

What should a token verifier never trust?

The algorithm or key identifier inside the token. The verifier decides what it accepts. Also compare signatures in constant time and decode the payload only after verification, or you are acting on attacker-controlled data.

Need something like this built?