Skip to content

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

@pinkpigeonltd

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

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