Next.js Discord

Discord Forum

Error: Cookies can only be modified in a Server Action or Route Handler.

Answered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
I need help with getting read of that error. It only appears the first time I try accessing my website for some reason. According to the warning the error is coming by the code in this file:
// utilities/supabase/server.ts

import {cookies} from "next/headers";
import {createServerClient} from "@supabase/ssr";

export function createClient(debug?: boolean) {
  return createServerClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL!,
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
    {
      global: {
        fetch: (...args) => {
          debug !== undefined && debug && console.log(args);

          return fetch(...args);
        }
      },
      cookies: {
        get(name: string) {
          // return cookie with the 'name' here    
          return cookies().get(name)?.value;      
        },
        set(name: string, value, options) {
          // set cookie
          cookies().set(name, value, options);
        },
        remove(name: string, options) {
          // remove cookie
          cookies().set(name, "", options);
        },       
      },
    }
  );  
};

If any additional information is required just let me know.
Answered by Sun bear
I solved it in the meantime.
View full answer

1 Reply

Sun bearOP
I solved it in the meantime.
Answer