Cookies
Set outbound cookies with ctx.cookies.set and read inbound cookies with ctx.cookies.get without hand-building Set-Cookie headers.
AI generated, pending review
You want a handler to set a cookie on the way out and read one on the way in. Cookies aren’t a field on FlareResponse. Set them on the request context with ctx.cookies.set(...), and the runtime drains them into Set-Cookie when it writes the response.
- Call
ctx.cookies.set(name, value, options?)to queue an outbound cookie. - Return a normal
FlareResponse(or a plain object). - Call
ctx.cookies.get(name)to read an inbound cookie.
import { FlareHost, FlareResponse } from "@flare-ts/core";import { node } from "@flare-ts/core/node";
const host = new FlareHost(node);
host.http.post("/login", (ctx) => { ctx.cookies.set("session", "abc123", { httpOnly: true, secure: true, sameSite: "Lax", path: "/", maxAge: 60 * 60 * 24, }); return new FlareResponse(200, { ok: true });});
host.http.get("/me", (ctx) => { const session = ctx.cookies.get("session"); if (session === undefined) { return new FlareResponse(401, { error: "no session" }); } return new FlareResponse(200, { session });});
const app = host.build();app.run();Cookie options
Section titled “Cookie options”CookieOptions is exported from @flare-ts/core:
| Option | Maps to |
|---|---|
httpOnly | HttpOnly |
secure | Secure |
sameSite | SameSite=Strict|Lax|None |
path | Path=... |
domain | Domain=... |
maxAge | Max-Age=... (seconds) |
expires | Expires=... (UTC) |
partitioned | Partitioned |
sameSite: "None" requires secure: true at compile time and runtime.
Clear a cookie
Section titled “Clear a cookie”ctx.cookies.delete("session", { path: "/" });Pass the same path (and domain if used) as when setting.
- Set cookies through
ctx.cookies, not by hand-buildingSet-Cookieon response headers. ctx.cookies.getAll()returns the full parsed record (lazy parse, cached).- Cookies set on the context survive returning a web
Response.