diff --git a/apps/dev/nextjs/app/client.tsx b/apps/dev/nextjs/app/client.tsx
new file mode 100644
index 00000000..40d6f905
--- /dev/null
+++ b/apps/dev/nextjs/app/client.tsx
@@ -0,0 +1,10 @@
+"use client"
+
+import { useEffect } from "react"
+
+export default function Client({ session }: any) {
+ useEffect(() => {
+ console.log(window.location)
+ })
+ return
Sign in}
+ signOut={Sign out }
+ />
+ )
+}
diff --git a/apps/dev/nextjs/app/loading.tsx b/apps/dev/nextjs/app/loading.tsx
deleted file mode 100644
index 740149a3..00000000
--- a/apps/dev/nextjs/app/loading.tsx
+++ /dev/null
@@ -1,7 +0,0 @@
-export default function Loading() {
- return (
- <>
- Loading...
- >
- )
-}
diff --git a/apps/dev/nextjs/app/page.tsx b/apps/dev/nextjs/app/page.tsx
index a1c50a6e..be7b1e45 100644
--- a/apps/dev/nextjs/app/page.tsx
+++ b/apps/dev/nextjs/app/page.tsx
@@ -1,6 +1,12 @@
-export default function Page() {
+import { auth } from "auth"
+import { headers } from "next/headers"
+import Client from "./client"
+
+export default async function Page() {
+ const session = await auth(headers())
return (
<>
+
NextAuth.js Example
This is an example site to demonstrate how to use{" "}
diff --git a/apps/dev/nextjs/auth.ts b/apps/dev/nextjs/auth.ts
index af55fbc4..c22a6ddf 100644
--- a/apps/dev/nextjs/auth.ts
+++ b/apps/dev/nextjs/auth.ts
@@ -4,15 +4,27 @@ import Facebook from "@auth/core/providers/facebook"
import GitHub from "@auth/core/providers/github"
import Google from "@auth/core/providers/google"
import Twitter from "@auth/core/providers/twitter"
+import Credentials from "@auth/core/providers/credentials"
-export const { handlers, auth } = NextAuth({
+export const { handlers, auth, getServerSession } = NextAuth({
debug: true,
- providers: [GitHub, Auth0, Facebook, Google, Twitter],
+ providers: [
+ GitHub,
+ Auth0,
+ Facebook,
+ Google,
+ Twitter,
+ Credentials({
+ credentials: { password: { label: "Password", type: "password" } },
+ authorize(c) {
+ if (c.password !== "password") return null
+ return { id: "test", name: "Test User", email: "test@example.com" }
+ },
+ }),
+ ],
callbacks: {
- async authorized({ request, auth }) {
- if (request.nextUrl.pathname === "/dashboard") {
- return !!auth.token
- }
+ async authorized({ request: { nextUrl }, auth }) {
+ if (nextUrl.pathname === "/dashboard") return !!auth.user
return true
},
},
diff --git a/apps/dev/nextjs/components/access-denied.js b/apps/dev/nextjs/components/access-denied.tsx
similarity index 88%
rename from apps/dev/nextjs/components/access-denied.js
rename to apps/dev/nextjs/components/access-denied.tsx
index 4c9c0d79..f8c83a73 100644
--- a/apps/dev/nextjs/components/access-denied.js
+++ b/apps/dev/nextjs/components/access-denied.tsx
@@ -1,4 +1,4 @@
-import { signIn } from "next-auth/react"
+import { signIn } from "@auth/nextjs/react"
export default function AccessDenied() {
return (
diff --git a/apps/dev/nextjs/components/footer.js b/apps/dev/nextjs/components/footer.js
deleted file mode 100644
index c9e3f1d4..00000000
--- a/apps/dev/nextjs/components/footer.js
+++ /dev/null
@@ -1,28 +0,0 @@
-import Link from "next/link"
-import styles from "./footer.module.css"
-import packageJSON from "@auth/nextjs/package.json"
-
-export default function Footer() {
- return (
-
- )
-}
diff --git a/apps/dev/nextjs/app/footer.tsx b/apps/dev/nextjs/components/footer.tsx
similarity index 100%
rename from apps/dev/nextjs/app/footer.tsx
rename to apps/dev/nextjs/components/footer.tsx
diff --git a/apps/dev/nextjs/components/header.js b/apps/dev/nextjs/components/header.js
deleted file mode 100644
index 0255d6a4..00000000
--- a/apps/dev/nextjs/components/header.js
+++ /dev/null
@@ -1,83 +0,0 @@
-import Link from "next/link"
-import { useSession } from "next-auth/react"
-import styles from "./header.module.css"
-
-// The approach used in this component shows how to built a sign in and sign out
-// component that works on pages which support both client and server side
-// rendering, and avoids any flash incorrect content on initial page load.
-export default function Header() {
- const { data: session, status } = useSession()
-
- return (
-
- )
-}
diff --git a/apps/dev/nextjs/components/header.module.css b/apps/dev/nextjs/components/header.module.css
index 773478c6..13e5ae1f 100644
--- a/apps/dev/nextjs/components/header.module.css
+++ b/apps/dev/nextjs/components/header.module.css
@@ -1,6 +1,7 @@
/* Set min-height to avoid page reflow while session loading */
.signedInStatus {
- display: block;
+ display: flex;
+ align-items: center;
min-height: 4rem;
width: 100%;
}
@@ -25,16 +26,13 @@
.signedInText,
.notSignedInText {
- position: absolute;
padding-top: 0.8rem;
- left: 1rem;
- right: 6.5rem;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
- display: inherit;
z-index: 1;
line-height: 1.3rem;
+ flex: 1;
}
.signedInText {
@@ -47,6 +45,7 @@
float: left;
height: 2.8rem;
width: 2.8rem;
+ margin-right: 1rem;
background-color: white;
background-size: cover;
background-repeat: no-repeat;
@@ -54,10 +53,11 @@
.button,
.buttonPrimary {
- float: right;
- margin-right: -0.4rem;
+ justify-self: end;
font-weight: 500;
border-radius: 0.3rem;
+ border: none;
+ font-weight: bold;
cursor: pointer;
font-size: 1rem;
line-height: 1.4rem;
diff --git a/apps/dev/nextjs/components/header.tsx b/apps/dev/nextjs/components/header.tsx
new file mode 100644
index 00000000..857f4091
--- /dev/null
+++ b/apps/dev/nextjs/components/header.tsx
@@ -0,0 +1,55 @@
+import Link from "next/link"
+import styles from "./header.module.css"
+
+export function Header({ session, signIn, signOut }: any) {
+ return (
+
+ )
+}
diff --git a/apps/dev/nextjs/components/layout.js b/apps/dev/nextjs/components/layout.js
deleted file mode 100644
index e3f4e48a..00000000
--- a/apps/dev/nextjs/components/layout.js
+++ /dev/null
@@ -1,12 +0,0 @@
-import Header from "components/header"
-import Footer from "components/footer"
-
-export default function Layout({ children }) {
- return (
- <>
-
- {children}
-
- >
- )
-}
diff --git a/apps/dev/nextjs/pages/_app.js b/apps/dev/nextjs/pages/_app.js
deleted file mode 100644
index 5a467b7a..00000000
--- a/apps/dev/nextjs/pages/_app.js
+++ /dev/null
@@ -1,10 +0,0 @@
-import { SessionProvider } from "next-auth/react"
-import "./styles.css"
-
-export default function App({ Component, pageProps }) {
- return (
-
-
-
- )
-}
diff --git a/apps/dev/nextjs/pages/_app.tsx b/apps/dev/nextjs/pages/_app.tsx
new file mode 100644
index 00000000..0332c6b1
--- /dev/null
+++ b/apps/dev/nextjs/pages/_app.tsx
@@ -0,0 +1,39 @@
+import {
+ SessionProvider,
+ signIn,
+ signOut,
+ useSession,
+} from "@auth/nextjs/react"
+import "./styles.css"
+import { Header } from "components/header"
+import styles from "components/header.module.css"
+import Footer from "components/footer"
+
+export default function App({ Component, pageProps }) {
+ return (
+
+
+
+
+
+ )
+}
+
+function PagesHeader() {
+ const { data: session } = useSession()
+ return (
+ signIn()} className={styles.buttonPrimary}>
+ Sign in
+
+ }
+ signOut={
+ signOut()} className={styles.button}>
+ Sign out
+
+ }
+ />
+ )
+}
diff --git a/apps/dev/nextjs/pages/api/examples/protected.ts b/apps/dev/nextjs/pages/api/examples/protected.ts
index dc9d2b76..415c4b62 100644
--- a/apps/dev/nextjs/pages/api/examples/protected.ts
+++ b/apps/dev/nextjs/pages/api/examples/protected.ts
@@ -1,9 +1,8 @@
// This is an example of to protect an API route
-import { authConfig } from "../auth-old/[...nextauth]"
-import { getServerSession } from "next-auth/next"
+import { getServerSession } from "auth"
export default async (req, res) => {
- const session = await getServerSession(req, res, authConfig as any)
+ const session = await getServerSession(req, res)
if (session) {
res.send({
diff --git a/apps/dev/nextjs/pages/credentials.js b/apps/dev/nextjs/pages/credentials.js
deleted file mode 100644
index 97656665..00000000
--- a/apps/dev/nextjs/pages/credentials.js
+++ /dev/null
@@ -1,67 +0,0 @@
-// eslint-disable-next-line no-use-before-define
-import * as React from "react"
-import { signIn, signOut, useSession } from "next-auth/react"
-import Layout from "components/layout"
-
-export default function Page() {
- const [response, setResponse] = React.useState(null)
- const handleLogin = (options) => async () => {
- if (options.redirect) {
- return signIn("credentials", options)
- }
- const response = await signIn("credentials", options)
- setResponse(response)
- }
-
- const handleLogout = (options) => async () => {
- if (options.redirect) {
- return signOut(options)
- }
- const response = await signOut(options)
- setResponse(response)
- }
-
- const { data: session } = useSession()
-
- if (session) {
- return (
-
- Test different flows for Credentials logout
- Default:
- Logout
-
- No redirect:
- Logout
-
- Response:
-
- {JSON.stringify(response, null, 2)}
-
-
- )
- }
-
- return (
-
- Test different flows for Credentials login
- Default:
-
- Login
-
-
- No redirect:
-
- Login
-
-
- No redirect, wrong password:
-
- Login
-
- Response:
-
- {JSON.stringify(response, null, 2)}
-
-
- )
-}
diff --git a/apps/dev/nextjs/pages/credentials.tsx b/apps/dev/nextjs/pages/credentials.tsx
new file mode 100644
index 00000000..7beaf020
--- /dev/null
+++ b/apps/dev/nextjs/pages/credentials.tsx
@@ -0,0 +1,67 @@
+import * as React from "react"
+import { signIn, signOut, useSession } from "@auth/nextjs/react"
+import { SignInResponse, SignOutResponse } from "@auth/nextjs/lib/client"
+
+export default function Page() {
+ const [response, setResponse] = React.useState<
+ SignInResponse | SignOutResponse
+ >()
+
+ const { data: session } = useSession()
+
+ if (session) {
+ return (
+ <>
+ Test different flows for Credentials logout
+ Default:
+ signOut()}>Logout
+
+ No redirect:
+ signOut({ redirect: false }).then(setResponse)}>
+ Logout
+
+
+ {response ? "Response:" : "Session:"}
+
+ {JSON.stringify(response ?? session, null, 2)}
+
+ >
+ )
+ }
+
+ return (
+ <>
+ Test different flows for Credentials login
+ Default:
+ signIn("credentials", { password: "password" })}>
+ Login
+
+
+ No redirect:
+
+ signIn("credentials", { redirect: false, password: "password" }).then(
+ setResponse
+ )
+ }
+ >
+ Login
+
+
+ No redirect, wrong password:
+
+ signIn("credentials", { redirect: false, password: "wrong" }).then(
+ setResponse
+ )
+ }
+ >
+ Login
+
+ Response:
+
+ {JSON.stringify(response, null, 2)}
+
+ >
+ )
+}
diff --git a/apps/dev/nextjs/pages/policy.js b/apps/dev/nextjs/pages/policy.js
index af95846b..04a76252 100644
--- a/apps/dev/nextjs/pages/policy.js
+++ b/apps/dev/nextjs/pages/policy.js
@@ -1,8 +1,6 @@
-import Layout from "../components/layout"
-
export default function Page() {
return (
-
+ <>
This is an example site to demonstrate how to use{" "}
Auth.js for authentication.
@@ -18,15 +16,11 @@ export default function Page() {
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Privacy Policy
-
- This site uses JSON Web Tokens and an in-memory database which resets
- every ~2 hours.
-
Data provided to this site is exclusively used to support signing in and
is not passed to any third party services, other than via SMTP or OAuth
for the purposes of authentication.
-
+ >
)
}
diff --git a/apps/dev/nextjs/pages/protected-ssr.js b/apps/dev/nextjs/pages/protected-ssr.js
index 545d5a1c..2a32e729 100644
--- a/apps/dev/nextjs/pages/protected-ssr.js
+++ b/apps/dev/nextjs/pages/protected-ssr.js
@@ -1,48 +1,34 @@
// This is an example of how to protect content using server rendering
-import { getServerSession } from "next-auth/next"
-import { authConfig } from "./api/auth-old/[...nextauth]"
-import Layout from "../components/layout"
-import AccessDenied from "../components/access-denied"
+import { getServerSession } from "auth"
+import AccessDenied from "components/access-denied"
export default function Page({ content, session }) {
// If no session exists, display access denied message
- if (!session) {
- return (
-
-
-
- )
- }
+ if (!session) return
// If session exists, display content
return (
-
+ <>
Protected Page
{content}
-
+ >
)
}
export async function getServerSideProps(context) {
- const session = await getServerSession(context.req, context.res, authConfig)
- let content = null
-
+ const session = await getServerSession(context.req, context.res)
if (session) {
+ // Note usually you don't need to fetch from an API route in getServerSideProps
+ // This is done here to demonstrate how you can fetch from a third-party API
+ // with a valid session. Likely you would also not pass cookies but an `Authorization` header
const hostname = process.env.NEXTAUTH_URL || "http://localhost:3000"
- const options = { headers: { cookie: context.req.headers.cookie } }
- const res = await fetch(`${hostname}/api/examples/protected`, options)
- const json = await res.json()
- if (json.content) {
- content = json.content
- }
+ const res = await fetch(`${hostname}/api/examples/protected`, {
+ headers: { cookie: context.req.headers.cookie },
+ })
+ return { props: { session, content: (await res.json()).content } }
}
- return {
- props: {
- session,
- content,
- },
- }
+ return { props: {} }
}