#Cookies set in Webinspector but not in code

5 messages · Page 1 of 1 (latest)

woven marten
#

When I try to access my cookies in an api route using next/headers cookies() function I just get back undefined, even though the cookie is defined in my browser

formal notchBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

neat obsidian
#

Can you share your code @woven marten

woven marten
#

Sure thing:

"use client";

import { useRouter } from "next/navigation";
import { signIn } from "./signIn.action";

export default function Home() {
  const router = useRouter();
  return (
    <div className="flex flex-col text-black bg-white shadow rounded h-[300px] w-[150px]">
      <button
        onClick={async () => {
          const { url } = await signIn();
          router.push(url);
        }}
        type="button"
      >
        Sign In with Google
      </button>
    </div>
  );
}
"use server";
import { google } from "@rapidnotes/auth";
import { generateCodeVerifier, generateState } from "arctic";
import { cookies } from "next/headers";

export const signIn = async () => {
  const state = generateState();
  const codeVerifier = generateCodeVerifier();

  cookies().set("state", state, {
    httpOnly: true,
    secure: process.env.NODE_ENV === "production",
    sameSite: "strict",
  });

  cookies().set("codeVerifier", codeVerifier, {
    httpOnly: true,
    secure: process.env.NODE_ENV === "production",
    sameSite: "strict",
  });

  const authUrl = await google.createAuthorizationURL(state, codeVerifier);

  return {
    url: authUrl.toString(),
  }
};

On the server I just do

cookies().get('state')?.value
cookies().get('codeVerifier')?.value
#

issue was the sameSite: strict setting