jlnstack

Usage

Basic cookie operations

Usage

Use createCookie to define a typed cookie. The function returns a cookie object with get, set, and delete methods.

import { createCookie } from "@jlnstack/cookies/next"

const sessionToken = createCookie<string>("session-token")
const token = await sessionToken.get()
// token: string | undefined

Returns undefined if the cookie doesn't exist or fails schema validation.

await sessionToken.set("abc123")

Pass options as the second argument to set:

await sessionToken.set("abc123", {
  maxAge: 60 * 60 * 24 * 7, // 7 days
  path: "/",
  secure: true,
  httpOnly: true,
  sameSite: "lax"
})

Available options:

OptionTypeDescription
maxAgenumberMax age in seconds
expiresDateExpiration date
pathstringCookie path
domainstringCookie domain
securebooleanHTTPS only
httpOnlybooleanHTTP only (not accessible via JS)
sameSite"strict" | "lax" | "none"SameSite policy
await sessionToken.delete()

JSON Values

Objects and arrays are automatically serialized to JSON:

const cart = createCookie<{ items: string[] }>("cart")

await cart.set({ items: ["product-1", "product-2"] })

const data = await cart.get()
// data: { items: string[] } | undefined

On this page