Skip to content

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 Updated 10 days ago · Flare 0.3

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.

  1. Call ctx.cookies.set(name, value, options?) to queue an outbound cookie.
  2. Return a normal FlareResponse (or a plain object).
  3. 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();

CookieOptions is exported from @flare-ts/core:

OptionMaps to
httpOnlyHttpOnly
secureSecure
sameSiteSameSite=Strict|Lax|None
pathPath=...
domainDomain=...
maxAgeMax-Age=... (seconds)
expiresExpires=... (UTC)
partitionedPartitioned

sameSite: "None" requires secure: true at compile time and runtime.

ctx.cookies.delete("session", { path: "/" });

Pass the same path (and domain if used) as when setting.

  • Set cookies through ctx.cookies, not by hand-building Set-Cookie on response headers.
  • ctx.cookies.getAll() returns the full parsed record (lazy parse, cached).
  • Cookies set on the context survive returning a web Response.