NextAuth v5 middleware results in webpack error
Unanswered
Masai Lion posted this in #help-forum
Masai LionOP
Hi! I am trying to migrate to NextAuth v5 but when changing my middleware from this:
To this:
Results in the error:
I am using
Thanks.
import { NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
import type { NextRequest } from "next/server";
export async function middleware(request: NextRequest) {
const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET,
cookieName: "next-auth.session-token",
});
if (!token) {
return NextResponse.redirect(new URL("/auth/login", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: "/dashboard/:path*",
};
To this:
import { auth } from "@/auth"
export default auth((req) => {
if (!req.auth) {
const newUrl = new URL("/auth/login", req.nextUrl.origin)
return Response.redirect(newUrl)
}
})
export const config = {
matcher: "/dashboard/:path*",
};
Results in the error:
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
I am using
Next.js 14.2.15
Thanks.