Skip to content

Middleware causes status 500 on deploy (runs fine locally) #6009

Closed

Description

@pinkpigeonltd

[REQUIRED] Environment info

firebase-tools:

12.4.0

Platform:

Manjaro
NextJS 13 (typescript)
I am also using next-firebase-auth-edge for authentication with Firebase and NextJS 13: https://github.com/awinogrodzki/next-firebase-auth-edge

[REQUIRED] Test case

This is the middleware:

import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { authentication } from "next-firebase-auth-edge/lib/next/middleware";
import { serverConfig, tokenConfig } from "./config/server-config";

const commonOptions = {
  ...tokenConfig,
  cookieSerializeOptions: {
    path: "/",
    httpOnly: true,
    secure: serverConfig.useSecureCookies, // Set this to true on HTTPS environments
    sameSite: "lax" as const,
    maxAge: 12 * 60 * 60 * 24 * 1000, // twelve days
  },
};

export async function middleware(request: NextRequest) {
  return authentication(request, {
    loginPath: "/api/login",
    logoutPath: "/api/logout",
    ...commonOptions,
    handleValidToken: async ({}) => {
      if (request.nextUrl.pathname === "/api/custom-claims") {
        const response = new NextResponse("", {
          status: 200,
          headers: { "content-type": "application/json" },
        });

        return response;
      }

      return NextResponse.next();
    },
    handleError: async (error) => {
      // Avoid redirect loop
      if (request.nextUrl.pathname === "/login") {
        return NextResponse.next();
      }

      const url = request.nextUrl.clone();
      url.pathname = "/login";
      url.search = `redirect=${request.nextUrl.pathname}${url.search}`;
      return NextResponse.redirect(url);
    },
  });
}

export const config = {
  matcher: ["/", "/((?!_next/static|favicon.ico|logo.svg).*)"],
};


[REQUIRED] Steps to reproduce

Follow the steps below, clone this public repo: https://github.com/pinkpigeonltd/minimal_example

Then:

npm install
firebase init (choosing only to setup hosting, nothing else needed)
firebase deploy (to a project of your choice)

You will see there is an error 500. The cloud console logs just seem to show "exited with status 1" which doesn't include much more info.

Remove middleware.ts and deploy again. The app shows up fine!

[REQUIRED] Expected behavior

For the app to work after deploying (or for the deployment process to see an error and alert the user that there's a problem)

[REQUIRED] Actual behavior

App returns error 500 with middleware.ts present

Activity

  1. JSON-FS commented on Jul 12, 2023

    @JSON-FS

    Hi, I am still encountering this issue. My firebase-tools version is 12.4.3. Same information as the OP - with middleware.ts, the app returns http 500, if I delete the middleware.ts then there are no problems. I guess the only difference with OP is that my app is freshly installed using npx create-next-app@latest just a day ago

    Also, just to add more information, my middleware.ts barely contains any code. It was just for testing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions