You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to integrate Edge-CSRF with next-connect (into a Next.js 14 app using app router) and in order to do so I need to access the response object from inside an edge router .use() function. What is the recommended way to modify the response from inside router.use()?
I want to be able to do something like this but it doesn't work because the response object isn't being passed between the middleware functions:
import{createCsrfProtect,CsrfError}from'@edge-csrf/nextjs';import{NextResponse}from'next/server';importtype{NextFetchEvent,NextRequest}from'next/server';import{createEdgeRouter}from'next-connect';constcsrfProtect=createCsrfProtect({cookie: {secure: process.env.NODE_ENV==='production',},});constrouter=createEdgeRouter<NextRequest,NextFetchEvent>();// Execute middleware before csrf protectionrouter.use(async(request,event,next)=>{constresponse=NextResponse.next();response.headers.set('X-BEFORE',newDate().toISOString())returnnext();});// Apply csrf protectionrouter.use(async(request,event,next)=>{constresponse=NextResponse.next();try{awaitcsrfProtect(request,response);}catch(err){if(errinstanceofCsrfError)returnnewNextResponse('invalid csrf token',{status: 403});throwerr;}returnnext();});// Execute middleware after csrf protectionrouter.use(async(request,event,next)=>{constresponse=NextResponse.next();response.headers.set('X-After',newDate().toISOString())returnnext();});// Define router fallback and export middlware functionrouter.all(()=>{returnNextResponse.next();});exportfunctionmiddleware(request: NextRequest,event: NextFetchEvent){returnrouter.run(request,event);}
The text was updated successfully, but these errors were encountered:
Hi,
I'm trying to integrate Edge-CSRF with next-connect (into a Next.js 14 app using app router) and in order to do so I need to access the response object from inside an edge router
.use()
function. What is the recommended way to modify the response from insiderouter.use()
?I want to be able to do something like this but it doesn't work because the response object isn't being passed between the middleware functions:
The text was updated successfully, but these errors were encountered: