Skip to content

Commit 08b95c8

Browse files
chriselingk-taro56ndom91
authored andcommitted
chore(docs): update migrating-to-v5.mdx client component example copy (nextauthjs#10747)
Co-authored-by: Kawahara Shotaro <[email protected]> Co-authored-by: Nico Domino <[email protected]>
1 parent 8371e27 commit 08b95c8

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

docs/pages/getting-started/migrating-to-v5.mdx

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,23 @@ export default async function Page() {
123123
</Tabs.Tab>
124124
<Tabs.Tab>
125125

126-
Imports from `next-auth/react` are now marked with the [`"use client"`](https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive) directive. Therefore, they can be used in client components just like they were used in previous versions.
126+
Imports from `next-auth/react` are now marked with the [`"use client"`](https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive) directive. Therefore, they can be used in client components just like they were used in previous versions. Don't forget, client components that attempt to access the session via context will need to be wrapped in a `<SessionProvider />`.
127127

128-
<Callout type="warning">
129-
If you have previously used `getSession()` or other imports from
130-
`next-auth/react` server-side, you'll have to refactor them to use `auth()` as
131-
shown above instead.
132-
</Callout>
128+
```ts filename="components/clientComponent.tsx"
129+
'use client';
130+
131+
import { useSession, SessionProvider } from 'next-auth/react';
132+
133+
const ClientComponent = () => {
134+
const session = useSession();
135+
136+
return (
137+
<SessionProvider>
138+
<p>Welcome {session?.user?.name}</p>
139+
</SessionProvider>
140+
)
141+
}
142+
```
133143

134144
</Tabs.Tab>
135145
<Tabs.Tab>
@@ -273,7 +283,12 @@ import authConfig from "./auth.config"
273283

274284
const prisma = new PrismaClient()
275285

276-
export const { handlers, auth } = NextAuth({
286+
export const {
287+
auth,
288+
handlers: { GET, POST },
289+
signIn,
290+
signOut,
291+
} = NextAuth({
277292
adapter: PrismaAdapter(prisma),
278293
session: { strategy: "jwt" },
279294
...authConfig,
@@ -285,7 +300,16 @@ export const { handlers, auth } = NextAuth({
285300
```ts filename="middleware.ts" {1} /NextAuth/
286301
import authConfig from "./auth.config"
287302
import NextAuth from "next-auth"
288-
export const { auth: middleware } = NextAuth(authConfig)
303+
304+
// Use only one of the two middleware options below
305+
// 1. Use middleware directly
306+
// export const { auth: middleware } = NextAuth(authConfig)
307+
308+
// 2. Wrapped middleware option
309+
const { auth } = NextAuth(authConfig);
310+
export default auth(async function middleware(req: NextRequest) {
311+
// Your custom middleware logic goes here
312+
}
289313
```
290314
291315
The above is just an example. **The main idea**, is to separate the part of the configuration that is edge-compatible from the rest, and only import the edge-compatible part in Middleware/Edge pages/routes. You can read more about this workaround in the [Prisma docs](/getting-started/adapters/prisma), for example.

0 commit comments

Comments
 (0)