Next.js Discord

Discord Forum

How to Set Cookies Manually in authorize Function with NextAuth.js v5?

Unanswered
Ojos Azules posted this in #help-forum
Open in Discord
Avatar
Ojos AzulesOP
Hi everyone,
I'm trying to implement a custom credentials provider in NextAuth.js v5 to sign in users using a username and password. The authentication works fine, but I'm running into an issue with cookies.

Here’s my situation:
In the authorize function, my external API sets a cookie in its response (via the Set-Cookie header).
However, this cookie is not reflected in the browser because the authorize function runs server-side, and the Set-Cookie header from the API response isn’t automatically applied to the browser.

How can I manually set this cookie for the browser inside the authorize function or elsewhere in the NextAuth flow? I want to ensure that the cookie from my API is properly set in the user's browser.
Image

3 Replies

Avatar
Great black wasp
first get set-cookie from header parse it of course then set cookie via cookies() from next/headers
Avatar
Ojos AzulesOP
It only sets the cookie on the Next.js server, not in the browser.
Avatar
Great black wasp
then the only way to to set it in async session and async jwt callback