nodemailer provider can't be used as middleware
Provider type
Environment
System:
OS: Linux 6.6 Manjaro Linux
CPU: (16) x64 AMD Ryzen 7 5800X 8-Core Processor
Memory: 23.45 GB / 31.27 GB
Container: Yes
Shell: 5.2.26 - /bin/bash
Binaries:
Node: 21.6.2 - /usr/bin/node
pnpm: 8.15.2 - /usr/bin/pnpm
npmPackages:
@auth/unstorage-adapter: ^2.0.0 => 2.1.0
next: latest => 14.2.3
next-auth: beta => 5.0.0-beta.18
react: ^18.2.0 => 18.3.1
Reproduction URL
https://github.com/arslan2012/nodemailer-test
Describe the issue
As soon as you add Nodemailer the auth.ts, middleware stops working, throwing below error
⨯ Error: The edge runtime does not support Node.js 'stream' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
or maybe this error
⨯ node_modules/.pnpm/[email protected]/node_modules/nodemailer/lib/base64/index.js (56:0) @ <unknown>
⨯ Class extends value undefined is not a constructor or null
null
disabling middleware or downgrading to next-auth 4 both fixes this issue. So this bug is next-auth 5 only.
How to reproduce
add Nodemailer provider to the auth.ts and enable middleware
Expected behavior
everything should work fine.
Please see the docs: https://authjs.dev/guides/edge-compatibility
But the error is occurring on my local machine, maybe I'm on the edge, my computer definitely isn't. per my Environment section, I'm using Node 21 on a Linux machine. @balazsorban44
After the configuration according to the document, the problem still exists。 https://authjs.dev/guides/edge-compatibility
After the configuration according to the document, the problem still exists。 https://authjs.dev/guides/edge-compatibility
i can confirm this, I also followed the instruction but error edge "stream" still show up
you can't use it. It is the dependency itself that has issues running in edge.
I found that the solution here was not the initial one i posted because
@kwatts22 was right, regardless of a custom email service, the email provider exported nodemailer at its root
so here's was the solution i opted for since I used jwt as a session signature.
import { NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
/**
* @type {import('next').NextConfig['experimental']}
*/
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|api/auth).*)",
],
};
const secret = process.env.NEXTAUTH_SECRET;
const publicRoutes = ["/login", "/signup"];
const authRoutes = ["/login"];
const DEFAULT_AUTHENTICATED_REDIRECT = "/dashboard";
export async function middleware(req) {
const { pathname } = req.nextUrl;
const token = await getToken({ req, secret });
const isAuthenticated = !!token;
if (publicRoutes.some((route) => pathname.startsWith(route))) {
return NextResponse.next();
}
if (isAuthenticated) {
if (authRoutes.includes(pathname)) {
const redirectUrl = new URL(DEFAULT_AUTHENTICATED_REDIRECT, req.url);
return NextResponse.redirect(redirectUrl);
}
return NextResponse.next();
}
const loginUrl = new URL("/login", req.url);
loginUrl.searchParams.set("callbackUrl", req.nextUrl.pathname);
return NextResponse.redirect(loginUrl);
}