Next.js Middleware config
how to setup
better-auth
middleware with next-intl
middleware https://next-intl.dev/docs/routing/middlewareMiddleware – Internationalization (i18n) for Next.js
Internationalization (i18n) for Next.js
1 Reply
i have setup better-auth middleware like this
and
and
/src/lib/auth/middleware.ts
import type { Session } from "better-auth";
import type { NextRequest, NextResponse } from "next/server";
import { auth } from "./server";
interface HandlerProps {
request: NextRequest;
session?: Session;
}
export function createAuthMiddleware(
handler: (props: HandlerProps) => Promise<NextResponse>,
) {
return async function middleware(request: NextRequest) {
const { headers } = request;
const session = (await auth.api.getSession({ headers }))?.session;
return handler({ request, session });
};
}
export function createRouteMatcher(routes: string[]) {
const regexes = routes.map((route) => new RegExp(`^${route}$`));
return (request: NextRequest) => {
const pathname = request.nextUrl.pathname;
return regexes.some((regex) => regex.test(pathname));
};
}
import type { Session } from "better-auth";
import type { NextRequest, NextResponse } from "next/server";
import { auth } from "./server";
interface HandlerProps {
request: NextRequest;
session?: Session;
}
export function createAuthMiddleware(
handler: (props: HandlerProps) => Promise<NextResponse>,
) {
return async function middleware(request: NextRequest) {
const { headers } = request;
const session = (await auth.api.getSession({ headers }))?.session;
return handler({ request, session });
};
}
export function createRouteMatcher(routes: string[]) {
const regexes = routes.map((route) => new RegExp(`^${route}$`));
return (request: NextRequest) => {
const pathname = request.nextUrl.pathname;
return regexes.some((regex) => regex.test(pathname));
};
}
/src/i18n/middleware.ts
import type { NextRequest } from "next/server";
import createMiddleware from "next-intl/middleware";
import { routing } from "./routing";
export function createI18nMiddleware(request: NextRequest) {
const handleI18nRouting = createMiddleware(routing);
return handleI18nRouting(request);
}
import type { NextRequest } from "next/server";
import createMiddleware from "next-intl/middleware";
import { routing } from "./routing";
export function createI18nMiddleware(request: NextRequest) {
const handleI18nRouting = createMiddleware(routing);
return handleI18nRouting(request);
}
/src/middleware.ts
import { type NextRequest, NextResponse } from "next/server";
import { createI18nMiddleware } from "@/i18n/middleware";
import {
createAuthMiddleware,
createRouteMatcher,
} from "@/lib/auth/middleware";
const isProtectedRoute = createRouteMatcher(["/dashboard(.*)"]);
const isAuthRoute = createRouteMatcher(["/auth(.*)"]);
const isPublicRoute = isAuthRoute || createRouteMatcher(["/"]);
export async function middleware(request: NextRequest) {
const authResponse = createAuthMiddleware(async ({ request, session }) => {
if (!session && isProtectedRoute(request)) {
const url = request.nextUrl.clone();
url.pathname = "/auth/signin";
url.searchParams.set("redirect", request.nextUrl.pathname);
return NextResponse.redirect(url);
}
if (session && isAuthRoute(request)) {
const url = request.nextUrl.clone();
url.pathname = "/dashboard";
return NextResponse.redirect(url);
}
return NextResponse.next({ request });
});
return isPublicRoute(request)
? createI18nMiddleware(request)
: authResponse(request);
}
export const config = {
runtime: "nodejs",
matcher: ["/((?!api|trpc|_next|_vercel|.*\\..*).*)"],
};
import { type NextRequest, NextResponse } from "next/server";
import { createI18nMiddleware } from "@/i18n/middleware";
import {
createAuthMiddleware,
createRouteMatcher,
} from "@/lib/auth/middleware";
const isProtectedRoute = createRouteMatcher(["/dashboard(.*)"]);
const isAuthRoute = createRouteMatcher(["/auth(.*)"]);
const isPublicRoute = isAuthRoute || createRouteMatcher(["/"]);
export async function middleware(request: NextRequest) {
const authResponse = createAuthMiddleware(async ({ request, session }) => {
if (!session && isProtectedRoute(request)) {
const url = request.nextUrl.clone();
url.pathname = "/auth/signin";
url.searchParams.set("redirect", request.nextUrl.pathname);
return NextResponse.redirect(url);
}
if (session && isAuthRoute(request)) {
const url = request.nextUrl.clone();
url.pathname = "/dashboard";
return NextResponse.redirect(url);
}
return NextResponse.next({ request });
});
return isPublicRoute(request)
? createI18nMiddleware(request)
: authResponse(request);
}
export const config = {
runtime: "nodejs",
matcher: ["/((?!api|trpc|_next|_vercel|.*\\..*).*)"],
};