Next.js Discord

Discord Forum

UnhandledSchemeError: Reading from "node:stream" is not handled by plugins (Unhandled scheme).

Unanswered
ABUL KALAM posted this in #help-forum
Open in Discord
I have used middleware.ts file in Next.js to persist login but I am getting there errors:
○ Compiling /src/middleware ...
 ⨯ node:stream
Module build failed: UnhandledSchemeError: Reading from "node:stream" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
Import trace for requested module:
node:stream
./node_modules/firebase-admin/node_modules/@fastify/busboy/lib/main.js
./node_modules/firebase-admin/lib/utils/api-request.js
./node_modules/firebase-admin/lib/auth/auth-api-request.js
./node_modules/firebase-admin/lib/auth/auth.js
./node_modules/firebase-admin/lib/auth/index.js
./node_modules/firebase-admin/lib/esm/auth/index.js

Here is my middleware.ts
import { NextRequest, NextResponse } from "next/server";
import { getAuth } from "firebase-admin/auth";

export async function middleware(req: NextRequest) {
  const sessionCookie = req.cookies.get("session")?.value;

  if (!sessionCookie) {
    return NextResponse.redirect(new URL("/login", req.url));
  }

  try {
    await getAuth().verifySessionCookie(sessionCookie, true);
    return NextResponse.next();
  } catch (error) {
    console.error("Failed to verify session cookie", error);
    // Redirect to login on failure
    return NextResponse.redirect(new URL("/login", req.url));
  }
}

// Apply middleware to specific routes
export const config = {
  matcher: ["/", "/admin"],
};

7 Replies

I am working with Next.js and Firebase and using Firebase auth. I have seen multiple approaches to persist login and I am not sure which one to use.
Please help out.
Currently, what I am doing is, when the user logs in, I set a cookie named session and then I have this middleware.
in nextjs, the runtime used to run middleware.ts is an "edge runtime" to make it run faster since it's called for every request. the edge runtime does not include the same APIs as node like the error you encountered: Reading from "node:stream" is not handled by plugins (Unhandled scheme)
you might want to take a look at this https://github.com/vercel/next.js/discussions/46722
and this is for the firebase part, someone seemed to have developed a solution for this https://github.com/vercel/next.js/discussions/33586
@iyxan23 and this is for the firebase part, someone seemed to have developed a solution for this https://github.com/vercel/next.js/discussions/33586
Now I have tried using next-firebase-auth-edge library to verify token. Now I am getting an issue with cookieSignatureKeys:
TypeError: Key for the RS256 algorithm must be of type CryptoKey. Received an instance of Uint8Array
I have tried generating keys using like 10 different methods but everytime, I get this issue.