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:
| Kind | Define with | The client can read the data | Use it when |
|---|---|---|---|
| JWS | defineJWSSession() | Yes, it is only signed | The data is not secret, or other services verify the token with a public key. |
| JWE | defineJWESession() | No, it is encrypted | The 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 framework | Start here |
|---|---|
Any handler that gets a Request and returns a Response | Read, update and clear |
| A client without cookies: a CLI, a service, a mobile app | Sessions without cookies |
| h3 | Sessions on h3 |
| Nitro | Sessions on Nitro |
| Nuxt | Sessions on Nuxt |
| Hono, Cloudflare Workers | Sessions on Hono and Workers |
| Another framework with its own cookie API | Write a driver, the full recipe |
| A session whose data lives in your own store | Build a session of your own |
#Where to go next
Configure a session
Key, lifetime, refresh, cookie name and attributes, and the session header.
Read, update and clear
Sign in, read the session, change it, sign out, and mint tokens without a request.
Hooks
Run your code when a session is read, issued, cleared, expires or fails, and rotate keys.
Sessions without cookies
Command-line tools, service accounts and apps that send the token in a header, and tokens in links.
Write a driver
Run sessions on a framework that exposes cookies its own way.
Build a session of your own
Another kind of session, such as one backed by your store, that reads and writes its token like the built-in ones.