Skip to content

Commit f457068

Browse files
F-47FGameInclaude
authored
docs: update middleware.ts references to proxy.ts for Next.js 16 (#13373)
Next.js 16 renamed middleware.ts to proxy.ts and the exported function from middleware to proxy. This updates all documentation references across installation, session protection, edge compatibility, migration, RBAC, passkey, and Prisma adapter pages to reflect the new convention. Co-authored-by: FGameIn <fares.galal@gamein.ae> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c7c2cfa commit f457068

File tree

9 files changed

+77
-54
lines changed

9 files changed

+77
-54
lines changed

docs/pages/concepts/index.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ Yes! Check out the [TypeScript docs](/getting-started/typescript)
5757

5858
</Accordion>
5959

60-
<Accordion title="Is Auth.js compatible with Next.js 12 Middleware?">
60+
<Accordion title="Is Auth.js compatible with Next.js Proxy / Middleware?">
6161

62-
[Next.js middleware](https://nextjs.org/docs/middleware) is supported and is recommended as of version 5. An example middleware setup can be found [here](/getting-started/session-management/protecting#nextjs-middleware).
62+
Yes! As of Next.js 16, `middleware.ts` has been renamed to `proxy.ts`. Auth.js supports both the new `proxy.ts` convention and the legacy `middleware.ts`. An example setup can be found [here](/getting-started/session-management/protecting#nextjs-proxy).
6363

6464
</Accordion>
6565
</Accordions>

docs/pages/getting-started/adapters/prisma.mdx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma
6565
</Callout>
6666

6767
<Callout type="warning">
68-
We recommend using version `@prisma/client@5.12.0` or above if using
69-
middleware or any other edge runtime(s). See [edge
70-
compatibility](#edge-compatibility) below for more information.
68+
We recommend using version `@prisma/client@5.12.0` or above if using proxy (or
69+
middleware in older Next.js versions) or any other edge runtime(s). In Next.js
70+
16+, `proxy.ts` runs on the Node.js runtime, so this may no longer be
71+
necessary. See [edge compatibility](#edge-compatibility) below for more
72+
information.
7173
</Callout>
7274

7375
<Code>
@@ -152,9 +154,9 @@ At the moment, Prisma is still working on being fully compatible with edge runti
152154
- Use the Prisma's [Accelerate](https://pris.ly/d/accelerate) feature
153155
- Follow our [Edge Compatibility](/guides/edge-compatibility) page as the workaround. This uses the `jwt` session strategy and separates the `auth.ts` configuration into two files.
154156

155-
Using Prisma with the `jwt` session strategy and `@prisma/client@5.9.1` or above doesn't require any additional modifications, other than ensuring you don't do any database queries in your middleware.
157+
Using Prisma with the `jwt` session strategy and `@prisma/client@5.9.1` or above doesn't require any additional modifications, other than ensuring you don't do any database queries in your proxy (or middleware in older Next.js versions).
156158

157-
Since `@prisma/client@5.9.1`, Prisma no longer throws about being incompatible with the edge runtime at instantiation, but at query time. Therefore, it is possible to import it in files being used in your middleware as long as you do not execute any queries in your middleware.
159+
Since `@prisma/client@5.9.1`, Prisma no longer throws about being incompatible with the edge runtime at instantiation, but at query time. Therefore, it is possible to import it in files being used in your proxy as long as you do not execute any queries in your proxy.
158160

159161
### Schema
160162

docs/pages/getting-started/installation.mdx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,15 @@ import { handlers } from "@/auth" // Referring to the auth.ts we just created
8686
export const { GET, POST } = handlers
8787
```
8888

89-
3. Add optional Middleware to keep the session alive, this will update the session expiry every time its called.
89+
3. Add optional Proxy to keep the session alive, this will update the session expiry every time its called.
9090

91-
```ts filename="./middleware.ts"
92-
export { auth as middleware } from "@/auth"
91+
<Callout type="info">
92+
As of Next.js 16, `middleware.ts` has been renamed to `proxy.ts`. If you are
93+
using an older version of Next.js, use `middleware.ts` with `export { auth as middleware }` instead.
94+
</Callout>
95+
96+
```ts filename="./proxy.ts"
97+
export { auth as proxy } from "@/auth"
9398
```
9499

95100
</Code.Next>

docs/pages/getting-started/migrating-to-v5.mdx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ npm install next-auth@beta
4242
- OAuth 1.0 support is deprecated.
4343
- The minimum required Next.js version is now [14.0](https://nextjs.org/14).
4444
- The import `next-auth/next` is replaced. See [Authenticating server-side](#authenticating-server-side) for more details.
45-
- The import `next-auth/middleware` is replaced. See [Authenticating server-side](#authenticating-server-side) for more details.
45+
- The import `next-auth/middleware` is replaced. See [Authenticating server-side](#authenticating-server-side) for more details. As of Next.js 16, `middleware.ts` has been renamed to `proxy.ts`.
4646
- When the `idToken: boolean` option is set to `false`, it won't entirely disable the ID token. Instead, it signals `next-auth` to also visit the `userinfo_endpoint` for the final user data. Previously, `idToken: false` opted out to check the `id_token` validity at all.
4747

4848
## Migration
@@ -92,7 +92,7 @@ See the table below for a summary of the changes. Below that are `diff` examples
9292
| Where | v4 | v5 |
9393
| ----------------------- | ----------------------------------------------------- | -------------------------------- |
9494
| **Server Component** | `getServerSession(authOptions)` | `auth()` call |
95-
| **Middleware** | `withAuth(middleware, subset of authOptions)` wrapper | `auth` export / `auth()` wrapper |
95+
| **Proxy (Middleware)** | `withAuth(middleware, subset of authOptions)` wrapper | `auth` export / `auth()` wrapper |
9696
| **Client Component** | `useSession()` hook | `useSession()` hook |
9797
| **Route Handler** | _Previously not supported_ | `auth()` wrapper |
9898
| **API Route (Edge)** | _Previously not supported_ | `auth()` wrapper |
@@ -145,27 +145,27 @@ const ClientComponent = () => {
145145
</Tabs.Tab>
146146
<Tabs.Tab>
147147

148-
```diff filename="middleware.ts"
148+
```diff filename="proxy.ts (middleware.ts in Next.js < 16)"
149149
- export { default } from "next-auth/middleware"
150-
+ export { auth as middleware } from "@/auth"
150+
+ export { auth as proxy } from "@/auth"
151151
```
152152

153-
For advanced use cases, you can use `auth` as a wrapper for your Middleware:
153+
For advanced use cases, you can use `auth` as a wrapper for your Proxy:
154154

155-
```ts filename="middleware.ts"
155+
```ts filename="proxy.ts"
156156
import { auth } from "@/auth"
157157

158-
export default auth((req) => {
158+
export const proxy = auth((req) => {
159159
// req.auth
160160
})
161161

162-
// Optionally, don't invoke Middleware on some paths
162+
// Optionally, don't invoke Proxy on some paths
163163
export const config = {
164164
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
165165
}
166166
```
167167

168-
Check out additional [Middleware docs](/getting-started/session-management/protecting#nextjs-middleware) for more details.
168+
Check out additional [Proxy docs](/getting-started/session-management/protecting#nextjs-proxy) for more details.
169169

170170
</Tabs.Tab>
171171
<Tabs.Tab>
@@ -258,7 +258,7 @@ While Auth.js strictly uses standard [Web APIs](https://developer.mozilla.org/en
258258

259259
Auth.js supports two [session strategies](/concepts/session-strategies). When you are using an adapter, it will default to the `database` strategy. **Unless your database and its adapter is compatible with the Edge runtime/infrastructure, you will not be able to use the `"database"` session strategy**.
260260

261-
So for example, if you are using an adapter that relies on an ORM/library that is not yet compatible with Edge runtime(s) below is an example where we force the `jwt` strategy and split up the configuration so the library doesn't attempt to access the database in edge environments, like in the middleware.
261+
So for example, if you are using an adapter that relies on an ORM/library that is not yet compatible with Edge runtime(s) below is an example where we force the `jwt` strategy and split up the configuration so the library doesn't attempt to access the database in edge environments, like in the proxy (or middleware for older Next.js versions).
262262

263263
<Callout>
264264
The following filenames are only a convention, they can be named anything you
@@ -291,24 +291,24 @@ export const { auth, handlers, signIn, signOut } = NextAuth({
291291
})
292292
```
293293

294-
3. In your middleware file, import the configuration object from your first `auth.config.ts` file and use it to lazily initialize Auth.js there. In effect, initialize Auth.js separately with all of your common options, but **without the edge incompatible adapter**.
294+
3. In your proxy file (or middleware file for older Next.js versions), import the configuration object from your first `auth.config.ts` file and use it to lazily initialize Auth.js there. In effect, initialize Auth.js separately with all of your common options, but **without the edge incompatible adapter**.
295295

296-
```ts filename="middleware.ts" {1} /NextAuth/
296+
```ts filename="proxy.ts" {1} /NextAuth/
297297
import authConfig from "./auth.config"
298298
import NextAuth from "next-auth"
299299

300-
// Use only one of the two middleware options below
301-
// 1. Use middleware directly
302-
// export const { auth: middleware } = NextAuth(authConfig)
300+
// Use only one of the two proxy options below
301+
// 1. Use proxy directly
302+
// export const { auth: proxy } = NextAuth(authConfig)
303303

304-
// 2. Wrapped middleware option
304+
// 2. Wrapped proxy option
305305
const { auth } = NextAuth(authConfig)
306-
export default auth(async function middleware(req: NextRequest) {
307-
// Your custom middleware logic goes here
306+
export const proxy = auth(async function proxy(req: NextRequest) {
307+
// Your custom proxy logic goes here
308308
})
309309
```
310310

311-
The above is just an example. **The main idea**, is to separate the part of the configuration that is edge-compatible from the rest, and only import the edge-compatible part in Middleware/Edge pages/routes. You can read more about this workaround in the [Prisma docs](/getting-started/adapters/prisma), for example.
311+
The above is just an example. **The main idea**, is to separate the part of the configuration that is edge-compatible from the rest, and only import the edge-compatible part in Proxy/Edge pages/routes. You can read more about this workaround in the [Prisma docs](/getting-started/adapters/prisma), for example.
312312

313313
Please follow up with your library/database/ORM's maintainer to see if they are planning to support the Edge runtime/infrastructure.
314314

docs/pages/getting-started/providers/passkey.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ CREATE UNIQUE INDEX Authenticator_credentialID_key ON Authenticator(credentialID
160160

161161
#### Edge Compatibility
162162

163-
If you're using `next-auth` with Next.js and middleware, you should ensure that your database client of choice is "edge compatible". If you're using an older version of Prisma or another adapter that is not edge compatible, you'll need to make some adjustments. Check out our [edge compatibility](/guides/edge-compatibility) guide for more details. There is also Prisma specific information in the [Prisma adapter docs](/getting-started/adapters/prisma#edge-compatibility).
163+
If you're using `next-auth` with Next.js and a proxy (or middleware in older versions), you should ensure that your database client of choice is "edge compatible" when using Next.js versions before 16. In Next.js 16+, `proxy.ts` runs on the Node.js runtime, so edge compatibility is no longer a concern. For older versions, check out our [edge compatibility](/guides/edge-compatibility) guide for more details. There is also Prisma specific information in the [Prisma adapter docs](/getting-started/adapters/prisma#edge-compatibility).
164164

165165
### Update Auth.js Configuration
166166

docs/pages/getting-started/session-management/protecting.mdx

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,19 @@ API Routes are protected in the same way as any other route in Express, see [the
240240
</Code.Express>
241241
</Code>
242242

243-
### Next.js Middleware
243+
### Next.js Proxy
244244

245-
With Next.js 12+, the easiest way to protect a set of pages is using the middleware file. You can create a `middleware.ts` file in your root pages directory with the following contents.
245+
With Next.js 16+, the easiest way to protect a set of pages is using the proxy file. You can create a `proxy.ts` file in your root pages directory with the following contents.
246246

247-
```ts filename="middleware.ts"
248-
export { auth as middleware } from "@/auth"
247+
<Callout type="info">
248+
As of Next.js 16, `middleware.ts` has been renamed to `proxy.ts` and the
249+
exported function has been renamed from `middleware` to `proxy`. If you are
250+
using an older version of Next.js, use `middleware.ts` and export `auth` as
251+
`middleware` instead.
252+
</Callout>
253+
254+
```ts filename="proxy.ts"
255+
export { auth as proxy } from "@/auth"
249256
```
250257

251258
Then define `authorized` callback in your `auth.ts` file. For more details check out the [reference docs](/reference/nextjs#authorized).
@@ -263,30 +270,30 @@ export const { auth, handlers } = NextAuth({
263270
})
264271
```
265272

266-
You can also use the `auth` method as a wrapper if you'd like to implement more logic inside the middleware.
273+
You can also use the `auth` method as a wrapper if you'd like to implement more logic inside the proxy.
267274

268-
```ts filename="middleware.ts"
275+
```ts filename="proxy.ts"
269276
import { auth } from "@/auth"
270277

271-
export default auth((req) => {
278+
export const proxy = auth((req) => {
272279
if (!req.auth && req.nextUrl.pathname !== "/login") {
273280
const newUrl = new URL("/login", req.nextUrl.origin)
274281
return Response.redirect(newUrl)
275282
}
276283
})
277284
```
278285

279-
You can also use a regex to match multiple routes or you can negate certain routes in order to protect all remaining routes. The following example avoids running the middleware on paths such as the favicon or static images.
286+
You can also use a regex to match multiple routes or you can negate certain routes in order to protect all remaining routes. The following example avoids running the proxy on paths such as the favicon or static images.
280287

281-
```ts filename="middleware.ts"
288+
```ts filename="proxy.ts"
282289
export const config = {
283290
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
284291
}
285292
```
286293

287-
Middleware will protect pages as defined by the `matcher` config export. For more details about the matcher, check out the [Next.js docs](https://nextjs.org/docs/pages/building-your-application/routing/middleware#matching-paths).
294+
The proxy will protect pages as defined by the `matcher` config export. For more details about the matcher, check out the [Next.js docs](https://nextjs.org/docs/app/api-reference/file-conventions/proxy).
288295

289296
<Callout>
290-
You should not rely on middleware exclusively for authorization. Always ensure
297+
You should not rely on the proxy exclusively for authorization. Always ensure
291298
that the session is verified as close to your data fetching as possible.
292299
</Callout>

0 commit comments

Comments
 (0)