Skip to Content

Session Hint

Let a page that is the same for everyone know whether asking for the session is worth a request

The session cookie is HttpOnly, so a page's own script cannot tell whether a visitor has one. A page rendered per request can ask on the server. A page that is the same for everyone (prerendered, cached at the edge) cannot, and a signed-in header on it has to ask from the browser, which means one /get-session request for every visitor, including the ones who never signed in and the crawlers that run scripts.

The session hint plugin keeps a second cookie beside the session cookie that a script can read. It says only that this browser was handed a session. It carries nothing of the session and proves nothing: use it to decide whether to ask, never to decide what to show.

Installation

Add the plugin to your auth config

auth.ts
import { entroAuth } from "@entro314labs/entro-auth"
import { sessionHint } from "@entro314labs/entro-auth/plugins"

export const auth = entroAuth({
    // ... other config options
    plugins: [
        sessionHint() 
    ]
})

Add the client plugin to your auth client

auth-client.ts
import { createAuthClient } from "@entro314labs/entro-auth/client"
import { sessionHintClient } from "@entro314labs/entro-auth/client/plugins"

export const authClient = createAuthClient({
    plugins: [
        sessionHintClient() 
    ]
})

Usage

Ask for the session only when the hint is there. In React, keep the component that calls useSession out of the tree until it is:

signed-in.tsx
"use client"

import { useSyncExternalStore, type ReactNode } from "react"
import { authClient } from "@/lib/auth-client"

// The hint does not change while a page is open: signing in and out load a page.
const subscribe = () => () => {}

function WithSession({ children }: { children: ReactNode }) {
    const { data: session } = authClient.useSession()
    return session ? children : null
}

export function SignedIn({ children }: { children: ReactNode }) {
    const hinted = useSyncExternalStore(
        subscribe,
        () => authClient.hasSessionHint(),
        () => false, // the server render, and the first client render, are signed out
    )
    return hinted ? <WithSession>{children}</WithSession> : null
}

When the page's chrome reads a hook instead (a header that asks isSignedIn in several places), give it a provider. Keep the component that calls useSession beside the children, not around them: wrapping them only when the hint is there would remount the whole page once the hint is read after hydration. The example is written for React 19 (use, <Context value>); on React 18 use useContext and <Context.Provider>.

session-provider.tsx
"use client"

import { createContext, use, useEffect, useState, useSyncExternalStore, type ReactNode } from "react"
import { authClient } from "@/lib/auth-client"

type State = { isLoaded: boolean; isSignedIn: boolean }
const NOT_KNOWN: State = { isLoaded: false, isSignedIn: false }
const SIGNED_OUT: State = { isLoaded: true, isSignedIn: false }
const SessionContext = createContext<State>(NOT_KNOWN)
const subscribe = () => () => {}

function Reader({ report }: { report: (state: State | null) => void }) {
    const { data: session, isPending } = authClient.useSession()
    const signedIn = Boolean(session)
    useEffect(() => {
        report(isPending ? null : { isLoaded: true, isSignedIn: signedIn })
    }, [isPending, signedIn, report])
    return null
}

export function SessionProvider({ children }: { children: ReactNode }) {
    // null on the server and for the first client render, where the cookie cannot be read yet
    const hinted = useSyncExternalStore<boolean | null>(subscribe, () => authClient.hasSessionHint(), () => null)
    const [read, setRead] = useState<State | null>(null)
    const state = hinted === null ? NOT_KNOWN : hinted ? (read ?? NOT_KNOWN) : SIGNED_OUT
    return (
        <SessionContext value={state}>
            {hinted && <Reader report={setRead} />}
            {children}
        </SessionContext>
    )
}

export const useAuth = () => use(SessionContext)

A visitor without the hint makes no request. A visitor with a stale hint (the session ran out, or was ended from another device) makes one, is answered with no session, and sees the signed-out page.

Use the hint to decide whether to ask, never to decide that somebody is signed out on a page the server already gated. If a server-side gate (a proxy or middleware that validates the session) sends signed-in visitors from the sign-in page to the dashboard, and the dashboard treats a missing hint as "signed out" and sends them back, a browser that holds the session cookie without the hint bounces between the two for ever. Inside a tree that is only served to signed-in browsers, read the session directly (for example a nested provider that mounts the reader unconditionally).

How it works

Whenever a response sets the session cookie, the plugin sets the hint with the same lifetime, path, domain and SameSite, and without HttpOnly. Whenever a response removes the session cookie (sign-out, a deleted account, a revoked session answering /get-session), it removes the hint.

A session that ends on the server while the browser is away leaves the hint behind until the next request that finds the session gone. That is the stale case above, and it costs one request.

Options

Both plugins take the same option, and it has to match:

  • cookieName (optional): the hint cookie's name. Defaults to entro-auth.session_hint.

What it is not

  • Not a session check. Anyone can set a cookie called entro-auth.session_hint in their own browser. All they gain is a /get-session request that answers nobody.
  • Not needed on a page rendered per request. There the server already knows: read the session with auth.api.getSession({ headers }) and render accordingly.

On this page