Sessions

Remember who a visitor is between requests with a signed or encrypted token in a cookie or a header, and nothing stored on the server.

A session here is a token the server writes and the client carries back. Think of a cloakroom ticket that has your coat's description printed on it and stamped by the cloakroom: the server needs no list of tickets, it reads the ticket and checks the stamp. The token travels in a cookie the browser sends with every request, in a request header an app sets, or in your hand when it arrives some other way, such as in a message body. Nothing is stored on the server, so there is nothing to look up and nothing to clean up. Concepts explains the token vocabulary.

#Signed or sealed

You pick one of two kinds when you define a session:

KindDefine withThe client can read the dataUse it when
JWSdefineJWSSession()Yes, it is only signedThe data is not secret, or other services verify the token with a public key.
JWEdefineJWESession()No, it is encryptedThe data is nobody else's business. A good default for a web app.

Either way, nobody can change the data without your key. A JWS is a postcard with a seal: anyone who holds it can read it. A JWE is a locked envelope.

#How a session runs

You define a session once, at start-up, with its key and lifetime. In each request you call use(event), which reads the token the request carries and hands you a session with its data. A visitor without a valid token gets an empty session whose id is undefined: no token exists until you call update(), typically at sign-in. update() issues a new token and, on a cookie session, sets the cookie on the response; clear() expires it again at sign-out. Expiry, an optional refresh of the cookie while the visitor is active, and the cookie layout are handled for you.

#Where the token comes from

Each request reads its token from one place, picked before the token is opened, and session.source says which. A cookie is what a browser sends back by itself, and the session writes the new cookie when the token changes. A header, named by sessionHeader, is what an app or a script sets on its own requests; the session writes nothing back, so a new token reaches the client in your response. A token in hand is one you pass to use({ token }) or open(), from a link, a message body or a queue. A request that sends both a header and a cookie holding the same token runs on the cookie; one whose header holds another token runs on the header alone. Configure a session has the full rule, and Sessions without cookies the flows that rely on it.

#Pick your framework

Your frameworkStart here
Any handler that gets a Request and returns a ResponseRead, update and clear
A client without cookies: a CLI, a service, a mobile appSessions without cookies
h3Sessions on h3
NitroSessions on Nitro
NuxtSessions on Nuxt
Hono, Cloudflare WorkersSessions on Hono and Workers
Another framework with its own cookie APIWrite a driver, the full recipe
A session whose data lives in your own storeBuild a session of your own

#Where to go next