Skip to content

Commit 65aa467

Browse files
committed
feat(nextjs): introduce next-auth v5 (#7443)
Next.js 13.4 [is out](https://nextjs.org/blog/next-13-4). For discussing project-related issues, please use #8487 The new version of NextAuth.js is based on `@auth/core`. If you want to test it out, you can do so already, installing `next-auth@experimental`: - **Documentation**: https://authjs.dev/reference/nextjs - **Migration guide**: https://authjs.dev/guides/upgrade-to-v5 BREAKING CHANGE: Follow the [migration guide](https://authjs.dev/guides/upgrade-to-v5)
1 parent 06cc2df commit 65aa467

File tree

261 files changed

+3614
-18092
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

261 files changed

+3614
-18092
lines changed

.github/pr-labeler.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ fauna: ["packages/adapter-fauna/**/*"]
1313
firebase: ["packages/adapter-firebase/**/*"]
1414
hasura: ["packages/adapter-hasura/**/*"]
1515
frameworks: ["packages/frameworks-*/**/*"]
16-
legacy: ["packages/next-auth/**/*"]
1716
mikro-orm: ["packages/adapter-mikro-orm/**/*"]
1817
mongodb: ["packages/adapter-mongodb/**/*"]
1918
neo4j: ["packages/adapter-neo4j/**/*"]
19+
next-auth: ["packages/next-auth/**/*"]
2020
pg: ["packages/adapter-pg/**/*"]
2121
playgrounds: ["apps/playgrounds/**/*"]
2222
pouchdb: ["packages/adapter-pouchdb/**/*"]

.gitignore

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ dist
3030
.cache-loader
3131
packages/next-auth/providers
3232
packages/next-auth/src/providers/oauth-types.ts
33-
packages/next-auth/client
34-
packages/next-auth/css
35-
packages/next-auth/utils
36-
packages/next-auth/core
37-
packages/next-auth/jwt
38-
packages/next-auth/react
39-
packages/next-auth/next
4033
packages/*/*.js
4134
packages/*/*.d.ts
4235
packages/*/*.d.ts.map
@@ -90,7 +83,13 @@ packages/core/providers
9083
packages/core/src/lib/pages/styles.ts
9184
docs/docs/reference/core
9285
docs/docs/reference/sveltekit
86+
docs/docs/reference/nextjs
9387

88+
# Next.js
89+
packages/next-auth/lib
90+
packages/next-auth/providers
91+
# copied from @auth/core
92+
packages/next-auth/src/providers
9493

9594
# SvelteKit
9695
packages/frameworks-sveltekit/index.*

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ coverage
3131
dist
3232
packages/**/*.cjs
3333
packages/**/*.js
34+
!packages/*/scripts/*.js
3435

3536
# @auth/core
3637
packages/core/src/providers/oauth-types.ts

.vscode/settings.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"files.exclude": {
33
"packages/core/{lib,providers,*.js,*.d.ts,*.d.ts.map}": true,
4-
"packages/next-auth/{client,core,css,jwt,next,providers,react,utils,*.js,*.d.ts}": true
54
},
65
"typescript.tsdk": "node_modules/typescript/lib",
76
"openInGitHub.remote.branch": "main"
8-
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { auth } from "auth"
2+
import { NextResponse } from "next/server"
3+
4+
export const GET = auth(function GET(req) {
5+
if (req.auth) return NextResponse.json(req.auth)
6+
return NextResponse.json({ message: "Not authenticated" }, { status: 401 })
7+
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { handlers } from "auth"
2+
const { GET: AuthGET, POST } = handlers
3+
export { POST }
4+
5+
import type { NextRequest } from "next/server"
6+
7+
// Showcasing advanced initialization in Route Handlers
8+
export async function GET(request: NextRequest) {
9+
// Do something with request
10+
const response = await AuthGET(request)
11+
// Do something with response
12+
return response
13+
}
14+
15+
// export const runtime = "edge"

apps/dev/nextjs/app/client.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use client"
2+
3+
import { signIn, useSession } from "next-auth/react"
4+
5+
export default function Client() {
6+
const { data: session, update, status } = useSession()
7+
return (
8+
<div>
9+
<pre>
10+
{status === "loading" ? "Loading..." : JSON.stringify(session, null, 2)}
11+
</pre>
12+
<button onClick={() => signIn("github")}>Sign in</button>
13+
<button onClick={() => update(`New Name`)}>Update session</button>
14+
</div>
15+
)
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <h1>This page is protected.</h1>
3+
}

apps/dev/nextjs/app/layout.tsx

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,56 @@
1-
export default function RootLayout({
2-
children,
3-
}: {
4-
children: React.ReactNode
5-
}) {
1+
import { auth, signIn, signOut, update } from "auth"
2+
import Footer from "components/footer"
3+
import { Header } from "components/header"
4+
import styles from "components/header.module.css"
5+
import "./styles.css"
6+
7+
export default function RootLayout(props: { children: React.ReactNode }) {
68
return (
79
<html>
8-
<head></head>
9-
<body>{children}</body>
10+
<body>
11+
<AppHeader />
12+
<main>{props.children}</main>
13+
<div>
14+
<form
15+
action={async () => {
16+
"use server"
17+
update({ user: { name: "New Name" } })
18+
}}
19+
>
20+
<button>Update name</button>
21+
</form>
22+
</div>
23+
<Footer />
24+
</body>
1025
</html>
1126
)
1227
}
28+
29+
export async function AppHeader() {
30+
const session = await auth()
31+
return (
32+
<Header
33+
session={session}
34+
signIn={
35+
<form
36+
action={async () => {
37+
"use server"
38+
await signIn("github")
39+
}}
40+
>
41+
<button className={styles.buttonPrimary}>Sign in</button>
42+
</form>
43+
}
44+
signOut={
45+
<form
46+
action={async () => {
47+
"use server"
48+
await signOut()
49+
}}
50+
>
51+
<button className={styles.button}>Sign out</button>
52+
</form>
53+
}
54+
/>
55+
)
56+
}

apps/dev/nextjs/app/page.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { SessionProvider } from "next-auth/react"
2+
import { auth } from "auth"
3+
import Client from "./client"
4+
5+
export default async function Page() {
6+
const session = await auth()
7+
return (
8+
<>
9+
{/*
10+
NOTE: The `auth()` result is not run through the `session` callback, be careful passing down data
11+
to a client component, this will be exposed via the /api/auth/session endpoint
12+
*/}
13+
<SessionProvider session={session} basePath="/auth">
14+
<Client />
15+
</SessionProvider>
16+
<h1>NextAuth.js Example</h1>
17+
<p>
18+
This is an example site to demonstrate how to use{" "}
19+
<a href="https://nextjs.authjs.dev">NextAuth.js</a> for authentication.
20+
</p>
21+
</>
22+
)
23+
}

0 commit comments

Comments
 (0)