Skip to content

Commit

Permalink
fix(ts): remove TS workaround for withAuth (#4926)
Browse files Browse the repository at this point in the history
* fix(ts): improve Middleware types

* docs: remove TS workaround for Middleware

* ignore lint

* simplify
  • Loading branch information
balazsorban44 committed Jul 15, 2022
1 parent bb664a2 commit 46eedee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
3 changes: 1 addition & 2 deletions apps/dev/middleware.ts
Expand Up @@ -30,12 +30,11 @@ export const config = { matcher: ["/middleware-protected"] }
// export default withAuth(
// function middleware(req, ev) {
// console.log(req, ev)
// return undefined // NOTE: `NextMiddleware` should allow returning `void`
// },
// {
// callbacks: {
// authorized: ({ token }) => token.name === "Balázs Orbán",
// }
// },
// }
// )

Expand Down
6 changes: 2 additions & 4 deletions docs/docs/configuration/nextjs.md
Expand Up @@ -177,13 +177,11 @@ If you do not define the options, NextAuth.js will use the default values for th
#### wrap middleware

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

export default withAuth(
// `withAuth` can augment your Request with the user's token.
function middleware(req: NextRequest & { nextauth: { token: JWT | null } }) {
// `withAuth` augments your `Request` with the user's token.
function middleware(req) {
console.log(req.nextauth.token)
},
{
Expand Down
27 changes: 19 additions & 8 deletions packages/next-auth/src/next/middleware.ts
Expand Up @@ -92,7 +92,9 @@ export interface NextAuthMiddlewareOptions {
secret?: string
}

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

async function handleMiddleware(
req: NextRequest,
Expand Down Expand Up @@ -145,12 +147,21 @@ async function handleMiddleware(
return NextResponse.redirect(signInUrl)
}

export interface NextRequestWithAuth extends NextRequest {
nextauth: { token: JWT | null }
}

export type NextMiddlewareWithAuth = (
request: NextRequestWithAuth,
event: NextFetchEvent
) => NextMiddlewareResult | Promise<NextMiddlewareResult>

export type WithAuthArgs =
| [NextRequest]
| [NextRequest, NextFetchEvent]
| [NextRequest, NextAuthMiddlewareOptions]
| [NextMiddleware]
| [NextMiddleware, NextAuthMiddlewareOptions]
| [NextRequestWithAuth]
| [NextRequestWithAuth, NextFetchEvent]
| [NextRequestWithAuth, NextAuthMiddlewareOptions]
| [NextMiddlewareWithAuth]
| [NextMiddlewareWithAuth, NextAuthMiddlewareOptions]
| [NextAuthMiddlewareOptions]
| []

Expand Down Expand Up @@ -178,9 +189,9 @@ export function withAuth(...args: WithAuthArgs) {
if (typeof args[0] === "function") {
const middleware = args[0]
const options = args[1] as NextAuthMiddlewareOptions | undefined
return async (...args: Parameters<NextMiddleware>) =>
return async (...args: Parameters<NextMiddlewareWithAuth>) =>
await handleMiddleware(args[0], options, async (token) => {
;(args[0] as any).nextauth = { token }
args[0].nextauth = { token }
return await middleware(...args)
})
}
Expand Down

1 comment on commit 46eedee

@vercel
Copy link

@vercel vercel bot commented on 46eedee Jul 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.