setCookie function

Set a cookie header in a Next.js API.

1 min read

views

Implementation

src/lib/setCookie.ts
import { serialize, CookieSerializeOptions } from "cookie";
import { NextApiResponse } from "next";
 
interface CookieOptions {
  res: NextApiResponse;
  name: string;
  value: unknown;
  /** expires in ms */
  expires: number;
}
 
export function setCookie(opts: CookieOptions) {
  const stringValue =
    typeof opts.value === "object" ? `j:${JSON.stringify(opts.value)}` : String(opts.value);
 
  const options: CookieSerializeOptions = {
    expires: new Date(Date.now() + opts.expires),
    httpOnly: true,
    secure: process.env.NODE_ENV === "production",
    path: "/",
    sameSite: "strict",
  };
 
  opts.res.setHeader("Set-Cookie", serialize(opts.name, String(stringValue), options));
}
ts

Usage

import { setCookie } from "~/lib/setCookie";
 
export default async function handler(req, res) {
  /* ... */
 
  const myValue = generateJWT({
    /* ... */
  });
  // expire after 3 hours
  setCookie({ res, name: "my-token", expires: 60 * 60 * 1000 * 3, value: myValue });
 
  /* ... */
}
ts