blomqma / next-rest-framework

Type-safe, self-documenting APIs for Next.js
https://next-rest-framework.vercel.app
Other
134 stars 17 forks source link

Cannot access NextRequest headers in framework middleware under Edge runtime #148

Closed jameslshannon closed 5 months ago

jameslshannon commented 5 months ago

Node 18.18.2, Next JS 14.1.3 and framework 5.1.11

RouteOperationDefinition excerpt:
<snip>
   .middleware((req: NextRequest) => {
      console.log(req.headers.get('Authorization'))
      return { request: req }
    })
    .handler(async (req, ctx, { request }) => {
        console.log(request.headers.get('Authorization'))
        console.log(req.headers.get('Authorization'))
<snip>

When I run the above code using Vercel dev under the standard Node runtime locally I get 3 successful logs of the Authorization header, but when I run it under the Edge runtime locally I only get the final log of the Authorization header, so for some reason the middleware NextRequest doesn't have the Authorization header in Edge runtime - even though my main Next middleware can access it fine. Other properties of the NextRequest object seem to be available in the middleware apart from any of the headers.

The framework middleware would be a great place to enforce handler-specific authorization rules if access to the request headers was supported.

Thanks,

James

blomqma commented 5 months ago

Thanks, this is about to get fixed in the next release with #144 that improves the support for handling forms. The request object is currently incorrectly cloned for the middleware functions causing the headers not be present there.

blomqma commented 5 months ago

After upgrading to 6.0.0-beta.1 you should see this addressed.