-
Hi there! I planned on using Firebase + Next.js Middlewares to check for user authentication. import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { auth } from "../../lib/firebase/admin";
export async function middleware(req: NextRequest) {
if (req.nextUrl.href.startsWith("/api/login")) {
const response = NextResponse.next();
const cookie = await auth.createSessionCookie(req.headers.get("token"), {
expiresIn: 9999999,
});
response.cookie("token", cookie, {
httpOnly: true,
sameSite: "strict",
secure: process.env.NODE_ENV === "production",
});
}
const user = await auth.verifySessionCookie(req.cookies.token);
return NextResponse.next();
} I get the following error: I've tried to install |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Yep, middleware doesn't support Node.js API. See https://nextjs.org/docs/api-reference/edge-runtime |
Beta Was this translation helpful? Give feedback.
-
Hey, I've recently released a library that aims to solve the problem: https://github.com/ensite-in/next-firebase-auth-edge It allows to create and verify tokens inside Next.js middleware and Next.js 13 server components. Built entirely upon Web Crypto API Follow the link for some code examples. |
Beta Was this translation helpful? Give feedback.
-
As @awinogrodzki noted, the "next-firebase-auth-edge" library addresses the Edge runtime <> Firebase issue. If you're interested, I wrote a guide on authenticating a NextJS app with Firebase while preserving SSR features: |
Beta Was this translation helpful? Give feedback.
Yep, middleware doesn't support Node.js API. See https://nextjs.org/docs/api-reference/edge-runtime