Skip to content

Commit

Permalink
Merge branch 'main' into feature/nonce-check-type
Browse files Browse the repository at this point in the history
  • Loading branch information
hamidbjss authored Jul 1, 2022
2 parents 19e9066 + 89d91ea commit 1d06a1d
Show file tree
Hide file tree
Showing 75 changed files with 5,535 additions and 7,137 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
git config --global user.name "Balázs Orbán"
pnpm release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
NPM_TOKEN_PKG: ${{ secrets.NPM_TOKEN_PKG }}
NPM_TOKEN_ORG: ${{ secrets.NPM_TOKEN_ORG }}
release-pr:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export { default } from "next-auth/middleware"

export const config = { matcher: ["/middleware-protected"] }

// Other ways to use this middleware

// import withAuth from "next-auth/middleware"
Expand Down
28 changes: 14 additions & 14 deletions apps/dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
},
"license": "ISC",
"dependencies": {
"@next-auth/fauna-adapter": "^1.0.1",
"@next-auth/prisma-adapter": "^1.0.1",
"@prisma/client": "^3.10.0",
"cpx": "^1.5.0",
"fake-smtp-server": "^0.8.0",
"faunadb": "^4.4.1",
"next": "^12.1.0",
"nodemailer": "^6.7.2",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"@next-auth/fauna-adapter": "^1",
"@next-auth/prisma-adapter": "^1",
"@prisma/client": "^3",
"faunadb": "^4",
"next": "12.1.7-canary.51",
"nodemailer": "^6",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@types/react": "^17.0.37",
"@types/react-dom": "^17.0.11",
"concurrently": "^7.1.0",
"prisma": "^3.10.0"
"@types/react": "^18",
"@types/react-dom": "^18",
"concurrently": "^7",
"cpx": "^1.5.0",
"fake-smtp-server": "^0.8.0",
"prisma": "^3"
}
}
4 changes: 2 additions & 2 deletions apps/dev/pages/api/examples/protected.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This is an example of to protect an API route
import { getSession } from "next-auth/react"
import { unstable_getServerSession } from "next-auth/next"

export default async (req, res) => {
const session = await getSession({ req })
const session = await unstable_getServerSession(req, res, options)

if (session) {
res.send({
Expand Down
4 changes: 2 additions & 2 deletions apps/dev/pages/api/examples/session.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This is an example of how to access a session from an API route
import { getSession } from "next-auth/react"
import { unstable_getServerSession } from "next-auth/next"

export default async (req, res) => {
const session = await getSession({ req })
const session = await unstable_getServerSession(req, res, authOptions)
res.send(JSON.stringify(session, null, 2))
}
8 changes: 6 additions & 2 deletions apps/dev/pages/protected-ssr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This is an example of how to protect content using server rendering
import { getServerSession } from "next-auth/next"
import { unstable_getServerSession } from "next-auth/next"
import { authOptions } from "./api/auth/[...nextauth]"
import Layout from "../components/layout"
import AccessDenied from "../components/access-denied"
Expand All @@ -26,7 +26,11 @@ export default function Page({ content, session }) {
}

export async function getServerSideProps(context) {
const session = await getServerSession(context, authOptions)
const session = await unstable_getServerSession(
context.req,
context.res,
authOptions
)
let content = null

if (session) {
Expand Down
22 changes: 15 additions & 7 deletions apps/dev/pages/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSession } from "next-auth/react"
import { unstable_getServerSession } from "next-auth/next"
import Layout from "../components/layout"

export default function Page() {
Expand All @@ -11,13 +11,17 @@ export default function Page() {
<Layout>
<h1>Server Side Rendering</h1>
<p>
This page uses the universal <strong>getSession()</strong> method in{" "}
<strong>getServerSideProps()</strong>.
This page uses the <strong>unstable_getServerSession()</strong> method
in <strong>getServerSideProps()</strong>.
</p>
<p>
Using <strong>getSession()</strong> in{" "}
<strong>getServerSideProps()</strong> is the recommended approach if you
need to support Server Side Rendering with authentication.
Using <strong>unstable_getServerSession()</strong> in{" "}
<strong>getServerSideProps()</strong> is currently the recommended
approach, although the API may still change, if you need to support
Server Side Rendering with authentication.
</p>
<p>
Using <strong>getSession()</strong> is still recommended on the client.
</p>
<p>
The advantage of Server Side Rendering is this page does not require
Expand All @@ -35,7 +39,11 @@ export default function Page() {
export async function getServerSideProps(context) {
return {
props: {
session: await getSession(context),
session: await unstable_getServerSession(
contex.req,
contex.res,
authOptions
),
},
}
}
1 change: 0 additions & 1 deletion apps/example-gatsby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ You **can** skip configuring a database and come back to it later if you want.
For more information about setting up a database, please check out the following links:

* Docs: [next-auth.js.org/adapters/overview](https://next-auth.js.org/adapters/overview)
* Adapters Repo: [nextauthjs/adapters](https://github.com/nextauthjs/adapters)

### 3. Configure Authentication Providers

Expand Down
6 changes: 3 additions & 3 deletions apps/example-gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"dependencies": {
"dotenv": "^16.0.0",
"gatsby": "next",
"next-auth": "^4.2.1",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"next-auth": "latest",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"vercel": "^23.1.2"
Expand Down
1 change: 0 additions & 1 deletion apps/example-nextjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ You **can** skip configuring a database and come back to it later if you want.
For more information about setting up a database, please check out the following links:

* Docs: [next-auth.js.org/adapters/overview](https://next-auth.js.org/adapters/overview)
* Adapters Repo: [nextauthjs/adapters](https://github.com/nextauthjs/adapters)

### 3. Configure Authentication Providers

Expand Down
12 changes: 12 additions & 0 deletions apps/example-nextjs/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { withAuth } from "next-auth/middleware"

// More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware
export default withAuth({
callbacks: {
authorized: ({ req, token }) =>
// /admin requires admin role, but /me only requires the user to be logged in.
req.nextUrl.pathname !== "/admin" || token?.userRole === "admin",
},
})

export const config = { matcher: ["/admin", "/me"] }
14 changes: 7 additions & 7 deletions apps/example-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
],
"license": "ISC",
"dependencies": {
"next": "^12.0.11-canary.4",
"next": "12.1.7-canary.51",
"next-auth": "latest",
"nodemailer": "^6.6.3",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"nodemailer": "^6",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@types/node": "^17.0.14",
"@types/react": "^17.0.39",
"typescript": "^4.5.5"
"@types/node": "^17",
"@types/react": "^18",
"typescript": "^4"
},
"prettier": {
"semi": false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Layout from "../../components/layout"
import Layout from "../components/layout"

export default function Page() {
return (
Expand Down
8 changes: 0 additions & 8 deletions apps/example-nextjs/pages/admin/_middleware.ts

This file was deleted.

10 changes: 6 additions & 4 deletions apps/example-nextjs/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NextAuth from "next-auth"
import NextAuth, { NextAuthOptions } from "next-auth"
import GoogleProvider from "next-auth/providers/google"
import FacebookProvider from "next-auth/providers/facebook"
import GithubProvider from "next-auth/providers/github"
Expand All @@ -9,7 +9,7 @@ import Auth0Provider from "next-auth/providers/auth0"

// For more information on each option (and a full list of options) go to
// https://next-auth.js.org/configuration/options
export default NextAuth({
export const authOptions: NextAuthOptions = {
// https://next-auth.js.org/configuration/providers/oauth
providers: [
/* EmailProvider({
Expand All @@ -18,7 +18,7 @@ export default NextAuth({
}),
// Temporarily removing the Apple provider from the demo site as the
// callback URL for it needs updating due to Vercel changing domains
Providers.Apple({
clientId: process.env.APPLE_ID,
clientSecret: {
Expand Down Expand Up @@ -60,4 +60,6 @@ export default NextAuth({
return token
},
},
})
}

export default NextAuth(authOptions)
7 changes: 4 additions & 3 deletions apps/example-nextjs/pages/api/examples/protected.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// This is an example of to protect an API route
import { getSession } from "next-auth/react"
import { unstable_getServerSession } from "next-auth/next"
import type { NextApiRequest, NextApiResponse } from "next"

export default async (req: NextApiRequest, res: NextApiResponse) => {
const session = await getSession({ req })
const session = await unstable_getServerSession(req, res, authOptions)

if (session) {
res.send({
Expand All @@ -12,7 +12,8 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
})
} else {
res.send({
error: "You must be signed in to view the protected content on this page.",
error:
"You must be signed in to view the protected content on this page.",
})
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useSession } from "next-auth/react"
import Layout from "../../components/layout"
import Layout from "../components/layout"

export default function MePage() {
const { data } = useSession()
Expand Down
2 changes: 0 additions & 2 deletions apps/example-nextjs/pages/me/_middleware.ts

This file was deleted.

22 changes: 10 additions & 12 deletions apps/example-nextjs/pages/server.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import { useSession, getSession } from "next-auth/react"
import { unstable_getServerSession } from "next-auth/next"
import { authOptions } from "./api/auth/[...nextauth]"
import Layout from "../components/layout"
import type { NextPageContext } from "next"

export default function ServerSidePage() {
export default function ServerSidePage({ session }) {
// As this page uses Server Side Rendering, the `session` will be already
// populated on render without needing to go through a loading stage.
// This is possible because of the shared context configured in `_app.js` that
// is used by `useSession()`.
const { data: session, status } = useSession()
const loading = status === "loading"

return (
<Layout>
<h1>Server Side Rendering</h1>
<p>
This page uses the universal <strong>getSession()</strong> method in{" "}
<strong>getServerSideProps()</strong>.
This page uses the <strong>unstable_getServerSession()</strong> method
in <strong>unstable_getServerSideProps()</strong>.
</p>
<p>
Using <strong>getSession()</strong> in{" "}
<strong>getServerSideProps()</strong> is the recommended approach if you
need to support Server Side Rendering with authentication.
Using <strong>unstable_getServerSession()</strong> in{" "}
<strong>unstable_getServerSideProps()</strong> is the recommended
approach if you need to support Server Side Rendering with
authentication.
</p>
<p>
The advantage of Server Side Rendering is this page does not require
Expand All @@ -38,7 +36,7 @@ export default function ServerSidePage() {
export async function getServerSideProps(context: NextPageContext) {
return {
props: {
session: await getSession(context),
session: await unstable_getServerSession(context.req, context.res, authOptions),
},
}
}
2 changes: 1 addition & 1 deletion apps/playground-sveltekit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"type": "module",
"dependencies": {
"cookie": "0.4.1",
"next-auth": "^4.3.3"
"next-auth": "workspace:*"
},
"prettier": {
"semi": false,
Expand Down
8 changes: 4 additions & 4 deletions apps/playground-sveltekit/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1232,10 +1232,10 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=

next-auth@^4.3.3:
version "4.3.3"
resolved "https://registry.yarnpkg.com/next-auth/-/next-auth-4.3.3.tgz#5ff892e73648a0f33c2af0e9d7cafda729f63ae7"
integrity sha512-bUs+oOOPT18Pq/+4v9q4PA/DGoVoAX6jwY7RTfE/akFXwlny+y/mNS6lPSUwpqcHjljqBaq34PQA3+01SdOOPw==
next-auth@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/next-auth/-/next-auth-4.5.0.tgz#2df57287fddc705b8971c88c60bad44a89ac6dd1"
integrity sha512-B6gYRIbqtj8nlDsx3y2Ruwp/mvZnItPs7VUULY43QYw+M9xtDPIM9EBZ3ryd/wNYA3MDteBJlzGm/ivseXcmJA==
dependencies:
"@babel/runtime" "^7.16.3"
"@panva/hkdf" "^1.0.1"
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/adapters/dgraph.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export default NextAuth({
encode: async ({ secret, token }) => {
return jwt.sign({...token, userId: token.id}, secret, {
algorithm: "HS256",
expiresIn: 30 * 24 * 60 * 60; // 30 days
expiresIn: 30 * 24 * 60 * 60, // 30 days
});
},
decode: async ({ secret, token }) => {
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/configuration/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ The redirect callback may be invoked more than once in the same flow.
This callback is called whenever a JSON Web Token is created (i.e. at sign
in) or updated (i.e whenever a session is accessed in the client). The returned value will be [encrypted](/configuration/options#jwt), and it is stored in a cookie.

Requests to `/api/auth/signin`, `/api/auth/session` and calls to `getSession()`, `useSession()` will invoke this function, but only if you are using a [JWT session](/configuration/options#session). This method is not invoked when you persist sessions in a database.
Requests to `/api/auth/signin`, `/api/auth/session` and calls to `getSession()`, `unstable_getServerSession()`, `useSession()` will invoke this function, but only if you are using a [JWT session](/configuration/options#session). This method is not invoked when you persist sessions in a database.

- As with database persisted session expiry times, token expiry time is extended whenever a session is active.
- The arguments _user_, _account_, _profile_ and _isNewUser_ are only passed the first time this callback is called on a new session, after the user signs in. In subsequent calls, only `token` will be available.
Expand Down
1 change: 1 addition & 0 deletions docs/docs/configuration/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The message object will contain:

- `user`: The user object from your adapter.
- `account`: The object returned from the provider.
- `profile`: The object returned from the `profile` callback of the OAuth provider.

### session

Expand Down
Loading

0 comments on commit 1d06a1d

Please sign in to comment.