Next.js Discord

Discord Forum

cookies cant be set unless in server actions or route handlers

Unanswered
Arinji posted this in #help-forum
Open in Discord
"use server";
import { cookies } from "next/headers";

export async function FormattedAuthCookie(token: string) {
  cookies().set("pb_auth", token);
}

I did add the use server thing on top, and whne i comment the cookies line the error fixes itself..

65 Replies

@Clown Click to see attachment
but its got the "use server" so it should be an action
What's the error then?
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
thats the issue, why is it showing that error lol
the flow is like this
i have a page which i call from
const pb = await initPocketBase();


import "server-only";

import { cookies } from "next/headers";
import Pocketbase from "pocketbase";
import { FormattedAuthCookie } from "./functions";

export async function initPocketBase() {
  const pb = new Pocketbase(process.env.NEXT_PUBLIC_POCKETBASE_URL);

  const authCookie = cookies().get("pb_auth")?.value;

  pb.authStore.loadFromCookie(authCookie!);

  try {
    pb.authStore.isValid && (await pb.collection("users").authRefresh());
  } catch (_) {
    pb.authStore.clear();
  }

  pb.authStore.onChange(() => {
    FormattedAuthCookie(pb.authStore.exportToCookie());
  });

  try {
    pb.authStore.isValid && (await pb.collection("users").authRefresh());
  } catch (_) {
    pb.authStore.clear();
  }

  return pb;
}
@Clown Click to see attachment
will a page calling the server action not work as well?
Should work
ahhhh im so confused with this error lol
Im not sure if its because of some weird bug or some specific reason its not working with a event
@Clown Im not sure if its because of some weird bug or some specific reason its not working with a event
ikr.... uh im thinking of just making the set part an api route
cause server actions are still experimental
so maybe thats the issue?
You can try it
Im gonna suggest some weird thing but maybe... maybe... try adding a 'use server' inside the top of onChange()
kk one sec
:lolsob: :lolsob:
"use server"
  pb.authStore.onChange(() => {
    FormattedAuthCookie(pb.authStore.exportToCookie());
  });
this or inside the function?
No no, inside
pb.authStore.onChange(() => {
"use server";
FormattedAuthCookie(pb.authStore.exportToCookie());
});
@Clown No no, inside
⨯ unhandledRejection: TypeError: Cannot read properties of undefined (reading 'token')
Check if pb.authStore.exportToCookie() is undefined
Its supposed to return a token right?
ye
it dosent even log out :/
weird
removing the "use server" makes it log out again...
@Giant panda The way you call `FormattedAuthCookies` implies it doesn't have to a be a server action. So don't do it.
oh, so make FormattedAuthCookies not have the "use server"?
Giant panda
Correct
@Giant panda Correct
still ocurring :/
the only 2 places calling the function as well
Giant panda
I assume you are calling initPocketBase from your page? I haven't worked with manually set cookies, so the following is speculation: I can imagine the possibility that you cannot set cookies from the server as part of a GET request to a page.
ye im calling it from a page
so yeet it into an api?
your logic does make sense, cause doing it from a page is like a GET request
Giant panda
As I was saying, it's just a gut feeling and I'd have to test it out myself, but you should try out other options to see whether you can make it work that way.
i have no clue what other options exist lol, my only guess is the server actions dont like how im calling stuff
Giant panda
Since you removed the use server directive you don't have actions anymore
btw @Clown the use server blocks every console log.... for some reason?
You could just use normal API routes
@Giant panda Since you removed the `use server` directive you don't have actions anymore
the initPocketbase still has it.. remove that as well?
Giant panda
Yeah
kk
Giant panda
It seems you don't know what server actions are. They are glorified API endpoints/route handlers that are called from the client-side, nothing else. You don't call a server action server-side.
And to add to that use server is not the opposite of use client.
You only use it when having actual server actions.
@Giant panda And to add to that `use server` is not the opposite of `use client`.
ye ik that, i just had the use server since i needed to access cookies
also removing it removes the error... but so does the cookies lol
wait my cookie got deleted, lemme re add it
Giant panda
I just got confirmation that cookies cannot be set as part of server component rendering just like I assumed.
uhhh elaborate pls :D
Giant panda
Which puts the "Good to know" into context.
oh so since we called it from a page.. we cant set cookies with a server action
Giant panda
cookies().set(...) only works in route handlers and server actions, basically whenever the client interacts with the exposed APIs and not the implicit server components.
sooo yeet the set cookie part into a route handler right
Giant panda
Which means you'd have to share in more detail what you are attempting in the first place to get help on figuring out a different approach of dealing with it.
oh i have a repo, one sec lemme revert all theese changes
Giant panda
You'd probably have to adjust the logic as you can only set a cookie within the middleware as I was reminded. Otherwise calling the route handler from server-side code would set the cookie for the server.
It's a bit unergonomic.