Nextjs Middleware authorization: Cannot append headers after they are sent to the client

I have a middleware that makes a request to a route handler endpoint to check if the user is authorized to view a certain page, however I’m getting this error Cannot append headers after they are sent to the client. I understand what it’s telling me but I’m not sure why it is happening. I’m following this post on how to implement this. The only difference between the post’s implementation and mine is that the post use Google Sign In and I’m using email/password, but I wouldn’t think that should matter.

middleware.ts

export async function middleware(request: NextRequest, response: NextResponse) {
  const session = request.cookies.get("session");

  //Return to /login if don't have a session
  if (!session) {
    return NextResponse.redirect(new URL("/", request.url));
  }

  //Call the authentication endpoint
  const responseAPI = await fetch("localhost:3000/api/login", {
    method: "GET",
    headers: {
      Cookie: `session=${session?.value}`,
    },
  });

  //Return to /login if token is not authorized
  if (responseAPI.status !== 200) {
    console.log("Not logged in");
    eeturn NextResponse.redirect(new URL("/login", request.url));
  }

  return NextResponse.next();
}

//Add your protected routes
export const config = {
  matcher: ["/admin"],
};

/api/login/route.ts

export async function GET(request: NextRequest) {
  const session = cookies().get("session")?.value || "";

  //Validate if the cookie exist in the request
  if (!session) {
    return NextResponse.json({ isLogged: false }, { status: 401 });
  }

  //Use Firebase Admin to validate the session cookie
  const decodedClaims = await auth.verifySessionCookie(session, true);

  if (!decodedClaims) {
    return NextResponse.json({ isLogged: false }, { status: 401 });
  }

  return NextResponse.json({ isLogged: true }, { status: 200 });
}

Here is the entire error stackgrace:

Middleware:  http://localhost:3000/admin
 ⨯ Error [TypeError]: fetch failed
    at context.fetch (file:///Users/Development/firebase/hosting/node_modules/next/dist/server/web/sandbox/context.js:292:38)
    at eval (webpack-internal:///(instrument)/./node_modules/@sentry/utils/esm/instrument/fetch.js:65:28)
    at Object.middleware$1 (webpack-internal:///(middleware)/./src/middleware.ts:22:31)
    at handlerResult.mechanism.type (webpack-internal:///(middleware)/./node_modules/@sentry/nextjs/esm/common/utils/edgeWrapperUtils.js:64:33)
    at handleCallbackErrors (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/utils/handleCallbackErrors.js:29:26)
    at eval (webpack-internal:///(middleware)/./node_modules/@sentry/nextjs/esm/common/utils/edgeWrapperUtils.js:63:111)
    at status.status (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/tracing/trace.js:84:13)
    at handleCallbackErrors (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/utils/handleCallbackErrors.js:29:26)
    at eval (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/tracing/trace.js:83:96)
    at eval (webpack-internal:///(instrument)/./node_modules/@sentry/vercel-edge/esm/async.js:54:14)
    at AsyncLocalStorage.run (node:async_hooks:338:14)
    at Object.withScope (webpack-internal:///(instrument)/./node_modules/@sentry/vercel-edge/esm/async.js:53:25)
    at withScope (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/currentScopes.js:66:18)
    at startSpan (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/tracing/trace.js:68:70)
    at eval (webpack-internal:///(middleware)/./node_modules/@sentry/nextjs/esm/common/utils/edgeWrapperUtils.js:52:75)
    at eval (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/tracing/trace.js:201:12)
    at eval (webpack-internal:///(instrument)/./node_modules/@sentry/vercel-edge/esm/async.js:54:14)
    at AsyncLocalStorage.run (node:async_hooks:338:14)
    at Object.withScope (webpack-internal:///(instrument)/./node_modules/@sentry/vercel-edge/esm/async.js:53:25)
    at withScope (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/currentScopes.js:72:14)
    at continueTrace (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/tracing/trace.js:198:70)
    at eval (webpack-internal:///(middleware)/./node_modules/@sentry/nextjs/esm/common/utils/edgeWrapperUtils.js:46:75)
    at eval (webpack-internal:///(instrument)/./node_modules/@sentry/vercel-edge/esm/async.js:76:14)
    at AsyncLocalStorage.run (node:async_hooks:338:14)
    at Object.withSetIsolationScope (webpack-internal:///(instrument)/./node_modules/@sentry/vercel-edge/esm/async.js:75:25)
    at withIsolationScope (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/currentScopes.js:104:16)
    at eval (webpack-internal:///(middleware)/./node_modules/@sentry/nextjs/esm/common/utils/edgeWrapperUtils.js:31:78)
    at eval (webpack-internal:///(middleware)/./node_modules/@sentry/nextjs/esm/common/utils/tracingUtils.js:94:16)
    at AsyncLocalStorage.run (node:async_hooks:338:14)
    at eval (webpack-internal:///(middleware)/./node_modules/@sentry/nextjs/esm/common/utils/tracingUtils.js:93:40)
    at eval (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/tracing/trace.js:222:12)
    at eval (webpack-internal:///(instrument)/./node_modules/@sentry/vercel-edge/esm/async.js:54:14)
    at AsyncLocalStorage.run (node:async_hooks:338:14)
    at Object.withScope (webpack-internal:///(instrument)/./node_modules/@sentry/vercel-edge/esm/async.js:53:25)
    at withScope (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/currentScopes.js:72:14)
    at withActiveSpan (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/tracing/trace.js:220:70)
    at eval (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/tracing/trace.js:260:12)
    at eval (webpack-internal:///(instrument)/./node_modules/@sentry/vercel-edge/esm/async.js:54:14)
    at AsyncLocalStorage.run (node:async_hooks:338:14)
    at Object.withScope (webpack-internal:///(instrument)/./node_modules/@sentry/vercel-edge/esm/async.js:53:25)
    at withScope (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/currentScopes.js:72:14)
    at startNewTrace (webpack-internal:///(middleware)/./node_modules/@sentry/core/esm/tracing/trace.js:257:70)
    at escapeNextjsTracing (webpack-internal:///(middleware)/./node_modules/@sentry/nextjs/esm/common/utils/tracingUtils.js:92:71)
    at Object.eval (webpack-internal:///(middleware)/./node_modules/@sentry/nextjs/esm/common/utils/edgeWrapperUtils.js:29:81)
    at Object.apply (webpack-internal:///(middleware)/./node_modules/@sentry/nextjs/esm/common/wrapMiddlewareWithSentry.js:23:10)
    at eval (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/adapter.js:202:31)
    at AsyncLocalStorage.run (node:async_hooks:338:14)
    at Object.wrap (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/async-storage/request-async-storage-wrapper.js:83:24)
    at eval (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/adapter.js:189:122)
    at eval (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/lib/trace/tracer.js:115:36)
 GET /admin 404 in 1ms
 ⨯ Error [ERR_HTTP_HEADERS_SENT]: Cannot append headers after they are sent to the client
    at ServerResponse.appendHeader (node:_http_outgoing:685:11)
    at AsyncLocalStorage.run (node:async_hooks:338:14)
digest: "3894424156"

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật