Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ts): remove TS workaround for withAuth #4926

Merged
merged 4 commits into from Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
29 changes: 21 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 @@ -180,8 +191,10 @@ export function withAuth(...args: WithAuthArgs) {
const options = args[1] as NextAuthMiddlewareOptions | undefined
return async (...args: Parameters<NextMiddleware>) =>
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
await handleMiddleware(args[0], options, async (token) => {
;(args[0] as any).nextauth = { token }
return await middleware(...args)
const [req, ...rest] = args
// @ts-expect-error
req.nextauth = { token }
return await middleware(req as NextRequestWithAuth, ...rest)
})
}

Expand Down