-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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)
- Loading branch information
1 parent
06cc2df
commit 65aa467
Showing
261 changed files
with
3,614 additions
and
18,092 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
{ | ||
"files.exclude": { | ||
"packages/core/{lib,providers,*.js,*.d.ts,*.d.ts.map}": true, | ||
"packages/next-auth/{client,core,css,jwt,next,providers,react,utils,*.js,*.d.ts}": true | ||
}, | ||
"typescript.tsdk": "node_modules/typescript/lib", | ||
"openInGitHub.remote.branch": "main" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { auth } from "auth" | ||
import { NextResponse } from "next/server" | ||
|
||
export const GET = auth(function GET(req) { | ||
if (req.auth) return NextResponse.json(req.auth) | ||
return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { handlers } from "auth" | ||
const { GET: AuthGET, POST } = handlers | ||
export { POST } | ||
|
||
import type { NextRequest } from "next/server" | ||
|
||
// Showcasing advanced initialization in Route Handlers | ||
export async function GET(request: NextRequest) { | ||
// Do something with request | ||
const response = await AuthGET(request) | ||
// Do something with response | ||
return response | ||
} | ||
|
||
// export const runtime = "edge" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"use client" | ||
|
||
import { signIn, useSession } from "next-auth/react" | ||
|
||
export default function Client() { | ||
const { data: session, update, status } = useSession() | ||
return ( | ||
<div> | ||
<pre> | ||
{status === "loading" ? "Loading..." : JSON.stringify(session, null, 2)} | ||
</pre> | ||
<button onClick={() => signIn("github")}>Sign in</button> | ||
<button onClick={() => update(`New Name`)}>Update session</button> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <h1>This page is protected.</h1> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,56 @@ | ||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
import { auth, signIn, signOut, update } from "auth" | ||
import Footer from "components/footer" | ||
import { Header } from "components/header" | ||
import styles from "components/header.module.css" | ||
import "./styles.css" | ||
|
||
export default function RootLayout(props: { children: React.ReactNode }) { | ||
return ( | ||
<html> | ||
<head></head> | ||
<body>{children}</body> | ||
<body> | ||
<AppHeader /> | ||
<main>{props.children}</main> | ||
<div> | ||
<form | ||
action={async () => { | ||
"use server" | ||
update({ user: { name: "New Name" } }) | ||
}} | ||
> | ||
<button>Update name</button> | ||
</form> | ||
</div> | ||
<Footer /> | ||
</body> | ||
</html> | ||
) | ||
} | ||
|
||
export async function AppHeader() { | ||
const session = await auth() | ||
return ( | ||
<Header | ||
session={session} | ||
signIn={ | ||
<form | ||
action={async () => { | ||
"use server" | ||
await signIn("github") | ||
}} | ||
> | ||
<button className={styles.buttonPrimary}>Sign in</button> | ||
</form> | ||
} | ||
signOut={ | ||
<form | ||
action={async () => { | ||
"use server" | ||
await signOut() | ||
}} | ||
> | ||
<button className={styles.button}>Sign out</button> | ||
</form> | ||
} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { SessionProvider } from "next-auth/react" | ||
import { auth } from "auth" | ||
import Client from "./client" | ||
|
||
export default async function Page() { | ||
const session = await auth() | ||
return ( | ||
<> | ||
{/* | ||
NOTE: The `auth()` result is not run through the `session` callback, be careful passing down data | ||
to a client component, this will be exposed via the /api/auth/session endpoint | ||
*/} | ||
<SessionProvider session={session} basePath="/auth"> | ||
<Client /> | ||
</SessionProvider> | ||
<h1>NextAuth.js Example</h1> | ||
<p> | ||
This is an example site to demonstrate how to use{" "} | ||
<a href="https://nextjs.authjs.dev">NextAuth.js</a> for authentication. | ||
</p> | ||
</> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { NextAuthConfig } from "next-auth" | ||
import Auth0 from "next-auth/providers/auth0" | ||
import Credentials from "next-auth/providers/credentials" | ||
import Facebook from "next-auth/providers/facebook" | ||
import GitHub from "next-auth/providers/github" | ||
import Google from "next-auth/providers/google" | ||
import Twitter from "next-auth/providers/twitter" | ||
|
||
declare module "next-auth" { | ||
/** | ||
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context | ||
*/ | ||
interface Session { | ||
user: { | ||
/** The user's postal address. */ | ||
address: string | ||
} & User | ||
} | ||
|
||
interface User { | ||
foo: string | ||
} | ||
} | ||
|
||
export default { | ||
debug: false, | ||
providers: [ | ||
GitHub({ account() {} }), | ||
Auth0, | ||
Facebook, | ||
Google, | ||
Twitter, | ||
Credentials({ | ||
credentials: { password: { label: "Password", type: "password" } }, | ||
authorize(c) { | ||
if (c.password !== "password") return null | ||
return { | ||
id: "test", | ||
foo: "bar", | ||
name: "Test User", | ||
email: "[email protected]", | ||
} | ||
}, | ||
}), | ||
], | ||
callbacks: { | ||
jwt({ token, trigger, session }) { | ||
if (trigger === "update") token.name = session.user.name | ||
return token | ||
}, | ||
}, | ||
} satisfies NextAuthConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import NextAuth from "next-auth" | ||
import Email from "next-auth/providers/email" | ||
import authConfig from "auth.config" | ||
import { PrismaClient } from "@prisma/client" | ||
import { PrismaAdapter } from "@auth/prisma-adapter" | ||
|
||
globalThis.prisma ??= new PrismaClient() | ||
|
||
// authConfig.providers.push( | ||
// // Start server with `pnpm email` | ||
// // @ts-expect-error | ||
// Email({ server: "smtp://127.0.0.1:1025?tls.rejectUnauthorized=false" }) | ||
// ) | ||
|
||
export const { handlers, auth, signIn, signOut, update } = NextAuth({ | ||
// adapter: PrismaAdapter(globalThis.prisma), | ||
session: { strategy: "jwt" }, | ||
...authConfig, | ||
}) |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
65aa467
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.
Successfully deployed to the following URLs:
auth-docs – ./docs
www.authjs.dev
sveltekit.authjs.dev
auth-docs-authjs.vercel.app
providers.authjs.dev
authjs.dev
next-auth-docs-m1mt.vercel.app
warnings.authjs.dev
errors.authjs.dev
auth-docs-git-main-authjs.vercel.app
adapters.authjs.dev