Middleware error on cluster deployment with Docker.

Hey guys I have a problem. My Nextjs app with better auth will not work correctly on my server enviroment. On vercel everything works perfect but on my server cluster it gets an 500 ssl error. This is my middleware code:
import { betterFetch } from "@better-fetch/fetch";
import { NextResponse, type NextRequest } from "next/server";


export default async function middleware(request: NextRequest) {
console.log("Origin url:", request.nextUrl.origin)
const { data: session } = await betterFetch<any>("/api/auth/get-session", {
baseURL: process.env.request.nextUrl.origin,
headers: {
cookie: request.headers.get("cookie") || "",
},
});
if (!session) {
return NextResponse.redirect(
new URL("/auth/login", request.nextUrl.origin),
);
}
if (session.user.role === "admin" && request.nextUrl.pathname === "/") {
return NextResponse.redirect(new URL("/admin", request.nextUrl.origin));
}
if (request.nextUrl.pathname.startsWith("/admin")) {
const isAdmin = session.user?.role === "admin";
if (!isAdmin) {
return NextResponse.redirect(new URL("/", request.nextUrl.origin));
}
}
return NextResponse.next();
}

export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt|auth/login|auth/register|auth/reset-password|images|impressum|datenschutz|agb).*)",
],
};
//
import { betterFetch } from "@better-fetch/fetch";
import { NextResponse, type NextRequest } from "next/server";


export default async function middleware(request: NextRequest) {
console.log("Origin url:", request.nextUrl.origin)
const { data: session } = await betterFetch<any>("/api/auth/get-session", {
baseURL: process.env.request.nextUrl.origin,
headers: {
cookie: request.headers.get("cookie") || "",
},
});
if (!session) {
return NextResponse.redirect(
new URL("/auth/login", request.nextUrl.origin),
);
}
if (session.user.role === "admin" && request.nextUrl.pathname === "/") {
return NextResponse.redirect(new URL("/admin", request.nextUrl.origin));
}
if (request.nextUrl.pathname.startsWith("/admin")) {
const isAdmin = session.user?.role === "admin";
if (!isAdmin) {
return NextResponse.redirect(new URL("/", request.nextUrl.origin));
}
}
return NextResponse.next();
}

export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt|auth/login|auth/register|auth/reset-password|images|impressum|datenschutz|agb).*)",
],
};
//
and the following error appears: at async (.next/server/src/middleware.js:13:32351) at async e4 (.next/server/src/middleware.js:13:29211) [cause]: [Error: 30A2D9D646700000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number: ../deps/openssl/openssl/ssl/record/ssl3_record.c:354 ] { library: 'SSL routines', reason: 'wrong version number', code: 'ERR_SSL_WRONG_VERSION_NUMBER' } Does anyone have any idea why Better-Auth is giving this error?
2 Replies
sebastian
sebastian4mo ago
You shouldn't put that logic into middleware. Session checks should be performed in the routes Middleware should check only for a cookie, not call db time every signle route possible
MATX
MATXOP4mo ago
Oh thanks. I didnt know that.

Did you find this page helpful?