Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

halvaradop
Copy link
Contributor

@halvaradop halvaradop commented Apr 14, 2025

☕️ Reasoning

This pull request updates the documentation in the migrating-to-v5 section by expanding and improving the existing content. A new Route Handlers tab was added to the details section, providing guidance on how to use NextAuth.js v5 with Route Handlers. Additionally, the Client 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

  • Updated the Client Component section
  • Added a new Route Handlers section
  • Ran the format:write script

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

🎫 Affected issues

#12767

📌 Resources

@halvaradop halvaradop requested a review from ndom91 as a code owner April 14, 2025 04:02
Copy link

vercel bot commented Apr 14, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
auth-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 21, 2025 11:36pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
next-auth-docs ⬜️ Ignored (Inspect) Visit Preview Apr 21, 2025 11:36pm

Copy link

vercel bot commented Apr 14, 2025

@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.
Copy link
Member

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?

Copy link
Contributor Author

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 }) 
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants