Next.js Discord

Discord Forum

Setting HTTP cookies and returning a page response

Unanswered
Lionhead posted this in #help-forum
Open in Discord
LionheadOP
Cookies can only be modified in a Server Action or Route Handler

I somewhat understand this, but then how do I set cookies THEN return a response with my layout and a page component, etc? I'm using the app directory and Next 13.

47 Replies

LionheadOP
how do i have my route /auth/callback handle setting a cookie AND returning a response ("logged in!" or "something happened!")
I'm not sure what you're asking? You can just set the cookie then send the response
LionheadOP
how
that is indeed what i'm trying to achieve, however i can't navigate the maze of docs to figure out how
LionheadOP
i can only use that from a server action or route handler
which one of those are you implying i use?
also, i saw this
what is startTransition, and where are the docs for it?
@Lionhead what is startTransition, and where are the docs for it?
You most likely don't need this, this is part of react. Are you just trying to set a cookie on certain/all page loads?
https://react.dev/reference/react/startTransition
LionheadOP
i'm trying to set a cookie on a certain route, /auth/callback and have that route return a page with my layout
LionheadOP
in pseudocode:

function Page() {
setCookie(...)
return #Unknown ChannelAuthenticated!</>
}
so i'd set it in the middleware then read whether the cookie got set to determine whether to return authenticated or not authenticated
Anything auth related should happen in middleware
LionheadOP
so i create a middleware.ts file in app/auth/callback/ directory?
@Lionhead so i'd set it in the middleware then read whether the cookie got set to determine whether to return authenticated or not authenticated
in a nutshell, yes. You do your token check in middleware, which runs before anything is sent to the page
LionheadOP
looks like there's a single middleware file
LionheadOP
thanks, i might
tbh I recommend it anyway, it's lightweight and easier than configuring it manually
LionheadOP
i'm not trying to protect any routes or anything like that
i really think i just want to implement it myself
i'm not using any existing identity provider
it looks like i could set cookies in getServerSideProps with the pages router
is there no equivalent for app router?
ah, i see how next auth does it, a route handler with a 302
ok then redirect to an error page with a query param with the error string
honestly setting a cookie before rendering a page shouldn't be this difficult
@Lionhead honestly setting a cookie before rendering a page shouldn't be this difficult
More important to ask why you're setting the cookie. You wouldn't typically need to just from a page.
LionheadOP
is there a way to render a page from a route handler
@Lionhead is there a way to render a page from a route handler
no, that's not something you would ever do
LionheadOP
well, suppose i don't want another round trip via sending a 302
how would i handle an oauth callback and return a page
@Lionhead how would i handle an oauth callback and return a page
That depends entirely on your authentication solution
but that would happen somewhere between a server action for the login and the redirect to the next page
It's not 1:1 returning a page with a response, you just direct to the next page
LionheadOP
ok let me try to rephrase the issue
how would you set a cookie that increments a number when a page has been accessed
every time i visit /foo/bar/baz i want to set a cookie that has a number that increments
You could do that in middleware, but you shouldn't use cookies for data storage because they get sent with every single subsequent request. Should generally just keep it to tokens or flags
LionheadOP
can you please stop telling me to not use cookies or use a certain library etc
it's very frustrating
if the answer is "you can only do it from middleware"
then that's fine
and, i suppose, more generally, does that mean you can only set headers for a page response from middleware?