Skip to content
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

chore(docs): update migrating-to-v5.mdx client component example copy #10747

Merged
merged 21 commits into from May 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1f4dc8c
Update migrating-to-v5.mdx
chriseling Apr 28, 2024
f4d9f2d
Merge branch 'main' into chriseling-patch-1
ndom91 Apr 28, 2024
a202152
Update migrating-to-v5.mdx
chriseling Apr 28, 2024
19774fb
Merge branch 'main' into chriseling-patch-1
ndom91 Apr 29, 2024
2a5f1c7
Merge branch 'main' into chriseling-patch-1
ndom91 Apr 29, 2024
4cd996a
Merge branch 'main' into chriseling-patch-1
ndom91 Apr 29, 2024
b98583e
Merge branch 'chriseling-patch-1' of github.com:chriseling/next-auth …
chriseling May 6, 2024
322036e
Update docs/pages/getting-started/migrating-to-v5.mdx
ndom91 May 19, 2024
4435082
Update docs/pages/getting-started/migrating-to-v5.mdx
ndom91 May 19, 2024
382467e
Update docs/pages/getting-started/migrating-to-v5.mdx
ndom91 May 19, 2024
329206f
Update docs/pages/getting-started/migrating-to-v5.mdx
ndom91 May 19, 2024
bb98d6d
Update docs/pages/getting-started/migrating-to-v5.mdx
ndom91 May 19, 2024
46f65cf
Update docs/pages/getting-started/migrating-to-v5.mdx
ndom91 May 19, 2024
9ffe5fd
Update docs/pages/getting-started/migrating-to-v5.mdx
ndom91 May 19, 2024
a06deb5
Merge branch 'main' into chriseling-patch-1
ndom91 May 19, 2024
a1b62c0
Update docs/pages/getting-started/migrating-to-v5.mdx
ndom91 May 19, 2024
8f2e029
Update docs/pages/getting-started/migrating-to-v5.mdx
ndom91 May 19, 2024
e7bd26b
Merge branch 'main' into chriseling-patch-1
ndom91 May 19, 2024
cf104bd
Update docs/pages/getting-started/migrating-to-v5.mdx
ndom91 May 19, 2024
f5a7c4e
Merge branch 'main' into chriseling-patch-1
ndom91 May 19, 2024
23f6eb5
Merge branch 'main' into chriseling-patch-1
ndom91 May 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 25 additions & 9 deletions docs/pages/getting-started/migrating-to-v5.mdx
Expand Up @@ -34,7 +34,7 @@ npm install next-auth@beta
#### Universal `auth()`

- A single method to authenticate anywhere
- Use `auth()` instead of `getServerSession`, `getSession`, `withAuth`, `getToken`, and `useSession` ([Read more](#authenticating-server-side))
- Use `auth()` instead of `getServerSession`, `getSession`, `withAuth`, `getToken` ([Read more](#authenticating-server-side))
ndom91 marked this conversation as resolved.
Show resolved Hide resolved

## Breaking Changes

Expand Down Expand Up @@ -74,8 +74,7 @@ Some things to note about the new configuration:
The old configuration file, contained in the API Route (`pages/api/auth/[...nextauth].ts` / `app/api/auth/[...nextauth]/route.ts`), now becomes much shorter. **These exports are designed to be used in an App Router API Route**, but the rest of your app can stay in the Pages Router if you are gradually migrating!

```ts filename="app/api/auth/[...nextauth]/route.ts"
import { handlers } from "@/auth"
export const { GET, POST } = handlers
export { GET, POST } from '@/auth';
chriseling marked this conversation as resolved.
Show resolved Hide resolved
```

## Authenticating server-side
Expand Down Expand Up @@ -125,11 +124,15 @@ export default async function Page() {

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.
chriseling marked this conversation as resolved.
Show resolved Hide resolved

<Callout type="warning">
If you have previously used `getSession()` or other imports from
`next-auth/react` server-side, you'll have to refactor them to use `auth()` as
shown above instead.
</Callout>
```ts filename="app/ClientComponent.tsx"
ndom91 marked this conversation as resolved.
Show resolved Hide resolved
import { useSession } from 'next-auth/react';
ndom91 marked this conversation as resolved.
Show resolved Hide resolved

const ClientComponent = () => {
const session = useSession();

return (<></>}
chriseling marked this conversation as resolved.
Show resolved Hide resolved
}
```

</Tabs.Tab>
<Tabs.Tab>
Expand Down Expand Up @@ -273,7 +276,12 @@ import authConfig from "./auth.config"

const prisma = new PrismaClient()

export const { handlers, auth } = NextAuth({
export const {
auth,
handlers: { GET, POST },
signIn,
signOut,
} = NextAuth({
adapter: PrismaAdapter(prisma),
session: { strategy: "jwt" },
...authConfig,
Expand All @@ -285,7 +293,15 @@ export const { handlers, auth } = NextAuth({
```ts filename="middleware.ts" {1} /NextAuth/
import authConfig from "./auth.config"
import NextAuth from "next-auth"

ndom91 marked this conversation as resolved.
Show resolved Hide resolved
// Use middleware directly
ndom91 marked this conversation as resolved.
Show resolved Hide resolved
export const { auth: middleware } = NextAuth(authConfig)
ndom91 marked this conversation as resolved.
Show resolved Hide resolved

// Wrapped middleware option, use one or the other but not both
ndom91 marked this conversation as resolved.
Show resolved Hide resolved
const { auth } = NextAuth(authConfig);
export default auth(async function middleware(req: NextRequest) {
// Your custom middleware logic goes here
}
```

The above is an example. The takeaway is to separate the part of the configuration that is edge-compatible from the rest, and only import that in Middleware/Edge pages/routes.
Expand Down