Skip to content

Commit 46eedee

Browse files
authoredJul 15, 2022
fix(ts): remove TS workaround for withAuth (#4926)
* fix(ts): improve Middleware types * docs: remove TS workaround for Middleware * ignore lint * simplify
1 parent bb664a2 commit 46eedee

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed
 

‎apps/dev/middleware.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ export const config = { matcher: ["/middleware-protected"] }
3030
// export default withAuth(
3131
// function middleware(req, ev) {
3232
// console.log(req, ev)
33-
// return undefined // NOTE: `NextMiddleware` should allow returning `void`
3433
// },
3534
// {
3635
// callbacks: {
3736
// authorized: ({ token }) => token.name === "Balázs Orbán",
38-
// }
37+
// },
3938
// }
4039
// )
4140

‎docs/docs/configuration/nextjs.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,11 @@ If you do not define the options, NextAuth.js will use the default values for th
177177
#### wrap middleware
178178

179179
```ts title="middleware.ts"
180-
import type { NextRequest } from "next/server"
181-
import type { JWT } from "next-auth/jwt"
182180
import { withAuth } from "next-auth/middleware"
183181

184182
export default withAuth(
185-
// `withAuth` can augment your Request with the user's token.
186-
function middleware(req: NextRequest & { nextauth: { token: JWT | null } }) {
183+
// `withAuth` augments your `Request` with the user's token.
184+
function middleware(req) {
187185
console.log(req.nextauth.token)
188186
},
189187
{

‎packages/next-auth/src/next/middleware.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ export interface NextAuthMiddlewareOptions {
9292
secret?: string
9393
}
9494

95-
type NextMiddlewareResult = ReturnType<NextMiddleware>
95+
// TODO: `NextMiddleware` should allow returning `void`
96+
// Simplify when https://github.com/vercel/next.js/pull/38625 is merged.
97+
type NextMiddlewareResult = ReturnType<NextMiddleware> | void // eslint-disable-line @typescript-eslint/no-invalid-void-type
9698

9799
async function handleMiddleware(
98100
req: NextRequest,
@@ -145,12 +147,21 @@ async function handleMiddleware(
145147
return NextResponse.redirect(signInUrl)
146148
}
147149

150+
export interface NextRequestWithAuth extends NextRequest {
151+
nextauth: { token: JWT | null }
152+
}
153+
154+
export type NextMiddlewareWithAuth = (
155+
request: NextRequestWithAuth,
156+
event: NextFetchEvent
157+
) => NextMiddlewareResult | Promise<NextMiddlewareResult>
158+
148159
export type WithAuthArgs =
149-
| [NextRequest]
150-
| [NextRequest, NextFetchEvent]
151-
| [NextRequest, NextAuthMiddlewareOptions]
152-
| [NextMiddleware]
153-
| [NextMiddleware, NextAuthMiddlewareOptions]
160+
| [NextRequestWithAuth]
161+
| [NextRequestWithAuth, NextFetchEvent]
162+
| [NextRequestWithAuth, NextAuthMiddlewareOptions]
163+
| [NextMiddlewareWithAuth]
164+
| [NextMiddlewareWithAuth, NextAuthMiddlewareOptions]
154165
| [NextAuthMiddlewareOptions]
155166
| []
156167

@@ -178,9 +189,9 @@ export function withAuth(...args: WithAuthArgs) {
178189
if (typeof args[0] === "function") {
179190
const middleware = args[0]
180191
const options = args[1] as NextAuthMiddlewareOptions | undefined
181-
return async (...args: Parameters<NextMiddleware>) =>
192+
return async (...args: Parameters<NextMiddlewareWithAuth>) =>
182193
await handleMiddleware(args[0], options, async (token) => {
183-
;(args[0] as any).nextauth = { token }
194+
args[0].nextauth = { token }
184195
return await middleware(...args)
185196
})
186197
}

1 commit comments

Comments
 (1)
Please sign in to comment.