Guides/Production patterns
Static pages without session requests
Keep anonymous visitors on public and prerendered pages from calling the auth server.
The problem
A public page that shows "Sign in" or the person's avatar has to find out who is there. A page that is the same for everyone (prerendered, or cached at the edge) cannot ask on the server, so its header asks /get-session from the browser: one request to the auth server for every visitor, including everyone who never signed in and every crawler that runs scripts. The session cookie is HttpOnly, so the page's script cannot check for it first.
When to use it
- A marketing site, documentation or a large static public tier beside a signed-in application.
- Pages rendered per request whose server helper should skip the auth server for requests with no session cookie.
In the browser: the session hint
The Session Hint plugin keeps a second, script-readable cookie beside the session cookie. It says only that this browser was given a session.
Add both plugins
import { entroAuth } from "@entro314labs/entro-auth";
import { sessionHint } from "@entro314labs/entro-auth/plugins";
export const auth = entroAuth({
plugins: [sessionHint()],
});import { createAuthClient } from "@entro314labs/entro-auth/client";
import { sessionHintClient } from "@entro314labs/entro-auth/client/plugins";
export const authClient = createAuthClient({
plugins: [sessionHintClient()],
});Mount the session reader only when the hint is there
Keep every useSession call out of the tree until authClient.hasSessionHint() is true. The plugin page has a component form (SignedIn) and a provider form for a header that reads a hook in several places: see Usage.
A visitor without the hint makes no request to the auth server. A visitor with a stale hint (the session ran out or was ended elsewhere) makes one request, is answered with no session, and the hint is removed.
On the server: no cookie, no call
A page or route rendered per request can do the same with the session cookie itself. getSessionCookie reads the cookie by name, with or without the __Secure- prefix, and does not validate it:
import { getSessionCookie } from "@entro314labs/entro-auth/cookies";
import { auth } from "./auth";
export async function getViewer(request: Request) {
if (!getSessionCookie(request)) return null; // nobody: the auth server is not asked
return auth.api.getSession({ headers: request.headers });
}Scheduled routes and anonymous page views send no session cookie, so they cost nothing. getSessionCookie does not read your configuration: if you changed the cookie prefix or name, pass them to it.
For people who are signed in, session.cookieCache reads the session from a signed cookie for a few minutes at a time instead of the database.
Pitfalls
- The hint decides whether to ask, never who is signed in. Anyone can set the cookie. Never gate content on it.
- Do not treat a missing hint as "signed out" on a page the server already gated. If a proxy that validates the session sends signed-in visitors from the sign-in page to the dashboard, and the dashboard sends visitors without the hint back, a browser that has the session cookie without the hint bounces between the two. Inside an area only served to signed-in browsers, read the session directly.
- Do not wrap the page in the session reader only when the hint is there. The hint is read after hydration, so wrapping the children conditionally remounts the whole page. Keep the reader beside the children, as the provider on the plugin page does.
- In Next.js, read
headers()before creating a lazily built auth instance.getAuth().api.getSession({ headers: await headers() })creates the instance first, andnext buildmay then prerender the page and fail on the missing settings. See the note under Next.js. - A route handler answers 401, not a redirect. A
fetchthat is redirected receives the sign-in page with a 200.