Authentication Middleware
The library offers an authMiddleware
function that’s meant to be used with Next.js Middleware (opens in a new tab).
For more details, check out the Authentication Middleware usage docs.
Here’s a basic example of how to use authMiddleware
in middleware.ts
:
middleware.ts
import type { NextRequest } from "next/server";
import { authMiddleware } from "next-firebase-auth-edge";
export async function middleware(request: NextRequest) {
return authMiddleware(request, {
loginPath: "/api/login",
logoutPath: "/api/logout",
apiKey: "XXxxXxXXXxXxxxxx_XxxxXxxxxxXxxxXXXxxXxX",
cookieName: "AuthToken",
cookieSignatureKeys: ["Key-Should-Be-at-least-32-bytes-in-length"],
cookieSerializeOptions: {
path: "/",
httpOnly: true,
secure: false, // Set this to true on HTTPS environments
sameSite: "lax" as const,
maxAge: 12 * 60 * 60 * 24, // Twelve days
},
serviceAccount: {
projectId: "your-firebase-project-id",
clientEmail: "firebase-adminsdk-nnw48@your-firebase-project-id.iam.gserviceaccount.com",
privateKey: "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
},
});
}
export const config = {
matcher: ["/api/login", "/api/logout", "/", "/((?!_next|favicon.ico|api|.*\\.).*)"],
};