Hey there,
I’m trying to use an Edge Function as middleware for a specific path.
Let’s say I have:
- Function A:
/api/create-post.ts - Function B:
/api/delete-post.ts
Both of these need an authentication layer. I’m using netlify.toml to register an Edge Function called auth that should run before any functions in /api/* according to Edge Functions declarations | Netlify Docs
[[edge_functions]]
pattern = "/api/(.*)"
excludedPattern = "/api/session"
function = "auth"
When a request hits /api/create-post.ts, the auth function runs first. It verifies the user token and is supposed to pass some user data to the next function via the context:
export default function Auth(req: Request, context: Context) {
const token = context.cookies.get("app-token");
if (isCool(token)) {
context.data = "some user data here";
return context.next(); // proceed to create-post.ts
}
return new Response("Not cool!", { status: 401 });
}
The issue is that neither Serverless Functions nor Edge Functions can actually modify the context in a way that persists to the next function. This makes it impossible to implement a proper middleware pattern without rolling our own solution or using a third-party library (like Middy).
So my question is: **Is there any plan to support this natively ?