Nextjs + Auth0 + Express

I wrote a middleware to redirect all my api requests (except /api/auth) to my express backend server. This works fine when I run it on my localhost (i.e. nodejs runtime), but gives a 401 (basically no auth token) when I deploy it to vercel (i.e. edge runtime).

import {
  getSession,
  withMiddlewareAuthRequired,
} from "@auth0/nextjs-auth0/edge";
import { NextRequest, NextResponse } from "next/server";
export { getSession };
export default withMiddlewareAuthRequired(async function middleware(
  req: NextRequest
) {
  if (req.nextUrl.pathname.startsWith("/api/auth")) {
    return;
  }
  const res = NextResponse.next();
  const user = await getSession(req, res);
  const token = user?.accessToken;
  if (token) {
    res.headers.set("Authorization", `Bearer ${token}`);
  }
  return res;
});

export const config = {
  matcher: ["/onboarding/:path*", "/api/:path*"],
};