-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(next-auth): provide request context in lazy initialization for auth()
calls in RSCs
#12653
base: main
Are you sure you want to change the base?
Conversation
- add createRequestFromHeaders utility for consistent request creation - pass constructed Request to config function instead of undefined - enable request-based config customization for RSCs, matching other contexts - support documented use cases like env-specific provider configuration - allow header/request access during server-side auth() calls
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
@huboh is attempting to deploy a commit to the authjs Team on Vercel. A member of the Team first needs to authorize it. |
Just wanted to share this while waiting for the merge, I had to implement a workaround in my company by forcing const { handlers, signIn, signOut, auth } = NextAuth(
async (req) => {
if (req) {
// code here will now have the request context for `auth()` calls inside RSCs
// in addition with old behaviour inside route handlers, middleware and api routes or getServerSideProps
} else {
// no request context for `signIn()`, `signOut()` and `unstable_update()` calls...No need for that ?
}
return {
...
}
}
);
const auth_ = async (...args: Parameters<typeof auth> | []) => {
if (args.length) {
return (
auth(...args)
);
} else {
// `auth()` was called inside rsc and lazy init function will now have request context
const heads = new Headers(await headers());
const reqUrl = (
(heads.get("x-url")) ||
(`${heads.get("x-forwarded-proto") || "http"}://${heads.get("host")}`)
);
const context: GetServerSidePropsContext = {
resolvedUrl: reqUrl,
query: {}, // not used by original `auth()`, so no need.
req: (
new NextRequest(reqUrl, { headers: heads }) as unknown as GetServerSidePropsContext["req"]
),
res: (
new NextResponse(null, { headers: heads }) as unknown as GetServerSidePropsContext["res"]
)
};
// Force RSC calls to use getServerSideProps path
return (
auth(context)
);
}
};
export {
handlers,
signOut,
signIn,
auth_ as auth
}; |
auth()
calls in RSCsauth()
calls in RSCs
auth()
calls in RSCsauth()
calls in RSCs
- Replace `Request` with `NextRequest` to match the expected type. - Ensure the request is properly initialized with headers, cookies and other fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed PR and description. In my opinion, this is not the direction that we want though. I would prefer to alter the function's parameter to support passing headers
, but not constructing the request object again without all the other context
since the RSC world discourages having a global context
from my understanding.
auth()
calls in RSCs, matching other contextsauth()
calls☕️ Reasoning
Lazy initialization is documented to provide request context access for customizing configuration, with examples like:
However, RSCs were receiving undefined instead of the request context, making these customizations impossible in RSC contexts. This fix makes
auth()
call in RSCs work consistently with other contexts (middleware, route handlers) and enables the documented use cases.Example use case that was broken but now works:
Previous Behavior
New Behavior
🧢 Checklist
🎫 Affected issues
📌 Resources