Next.js Discord

Discord Forum

Cookies can only be modified in a Server Action or Route Handler with Supabase Server Action Client

Unanswered
Gyrfalcon posted this in #help-forum
Open in Discord
GyrfalconOP
I'm using supabase for auth in my application everything is working fine, but when I'm trying to update any supabase user metadata, then I keep on getting the cookies error. You can find my code below:

import { createUser, retrieveUser } from "@/actions/user";
import { Chat } from "@/components/Chat";
import { getUser, updateUser } from "@/libs/auth";
import { randomUUID } from "crypto";
import { redirect } from "next/navigation";

const ChatPage = async () => {
    const {
        data: { session },
    } = await getUser();

    const userEmail = session?.user.email;

    const { data, error } = await updateUser({
        isSavedInDb: true,
        dbUserId: "123",
    });

    if (session) {
        const id = randomUUID();

        return <Chat id={id} initialMessages={[]} />;
    } else {
        redirect("/sign-in");
    }
};

export default ChatPage;


export const updateUser = async (payload: Object) => {
    "use server";

    const supabase = createServerActionClient({ cookies });

    const { data, error } = await supabase.auth.updateUser({
        data: {
            ...payload,
        },
    });

    console.log({ data, error });

    return { data, error };
};


I keep on getting Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options error.

As per the docs cookies must be set inside of server action or route handler and I've done exactly the same in my code as well but I still keep on getting the same error. How can I fix it?

1 Reply

Northeast Congo Lion
Do you use the ssr package by supabase? xd