-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
chore(docs): update migrating-to-v5
docs
#12872
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
base: main
Are you sure you want to change the base?
chore(docs): update migrating-to-v5
docs
#12872
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
@halvaradop is attempting to deploy a commit to the authjs Team on Vercel. A member of the Team first needs to authorize it. |
@@ -170,6 +180,31 @@ Check out additional [Middleware docs](/getting-started/session-management/prote | |||
</Tabs.Tab> | |||
<Tabs.Tab> | |||
|
|||
As of `NextAuth.js v5`, you can use the `auth()` function inside [Route Handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers) to access the session. |
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.
in Route handler, we can just use the req.auth
like the other part of the docs here: https://authjs.dev/reference/nextjs#in-route-handlers
I think we can just link to this page here, HDYT?
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.
You're right, however, this method of accessing the session isn't working as expected. It's been reported by users that req.auth
often returns null
. Because of this, I’ve chosen to access the session directly using the auth()
function instead of the wrapper.
Test Case
I created a demo using the Credentials
provider. After logging in, I set up an endpoint to access the session via the wrapper. During testing, all values passed through the Credentials
provider are authenticated correctly. The session is properly stored, and cookies are created with the authjs
prefix.
When accessing the session using the auth()
function directly—or via useSession()
—everything works as expected. However, accessing the session through request.auth
consistently returns null
.
Tell me what you think, and I’ll make the necessary changes.
export const GET = auth((request) => {
const session = request.auth
if(!session) {
return NextResponse.json(
{ message: "No session" },
{ status: 401 }
)
}
return NextResponse.json({
auth: session,
message: "GET /api/session"
}, { status: 200 })
})
☕️ Reasoning
This pull request updates the documentation in the
migrating-to-v5
section by expanding and improving the existing content. A newRoute Handlers
tab was added to the details section, providing guidance on how to use NextAuth.js v5 with Route Handlers. Additionally, theClient Component
section was revised to address multiple issues raised by the community regarding unclear documentation.The
format:write
script was also run to ensure consistent formatting across the documentation files.It’s been a while since my last contribution to the project, so I’m glad to be back and help improve the docs.
Key Changes
Client Component
sectionRoute Handlers
sectionformat:write
script🧢 Checklist
🎫 Affected issues
#12767
📌 Resources