Skip to content

How to set a custom cookie with auth middleware and retain behavior? #11692

Answered by theogravity
theogravity asked this question in Help
Discussion options

You must be logged in to vote

I figured it out. This is because in the nextAuth function, there's a branch specifically for custom middleware:

https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/lib/index.ts#L261

  } else if (userMiddlewareOrRoute) {
    // Execute user's middleware/handler with the augmented request
    const augmentedReq = request as NextAuthRequest
    augmentedReq.auth = auth
    response =
      (await userMiddlewareOrRoute(augmentedReq, args[1])) ??
      NextResponse.next()

and a branch for a not authorized situation with no custom middleware:

https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/lib/index.ts#L268

  } else if (!authorized) {
    const sig…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by theogravity
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
1 participant