set cookie manually in next.js server action

"use server";

import { auth } from "@/auth";
import { cookies } from "next/headers";

export async function signInAction({
  email,
  password,
}: {
  email: string;
  password: string;
}) {
  const res = await auth.api.signInEmail({
    body: {
      email,
      password,
    },
    asResponse: true,
  });

 

  const cookieStore = await cookies();
  const sessionCookieFull = res.headers.get("set-cookie");
  if (sessionCookieFull) {
    const [sessionCookie, _maxAge, _path, _httpOnly, _sameSite] =
      sessionCookieFull.split(";").map((part) => part.trim());

    const [key, value] = sessionCookie.split("=");
    const maxAge = _maxAge.split("=")[1];
    const path = _path.split("=")[1];
    const httpOnly = Boolean(_httpOnly.split("=")[1]);
    const sameSite = _sameSite.split("=")[1];
    console.log({ key, value, maxAge, path, httpOnly, sameSite });
    cookieStore.delete(key);
    cookieStore.set(key, value, {
      maxAge: Number(maxAge),
      path,
      httpOnly: true,
      ...(sameSite === "Lax" ? { sameSite: "lax" } : {}),
    });
  }
}


I know there is a plugin to do this for me but i wanted to see if i could do it manually, i compared it to the cookie when i use the client instance and it seems pretty similar but my session is still undefined

do i have to use the plugin or am i missing something? i think i normalized enough to properly set the cookie
Solution
okay figured it out

I needed to use decodeURIComponent on the value
Was this page helpful?