pelagia-portal/App/middleware.ts
2026-05-18 23:18:58 +05:30

24 lines
686 B
TypeScript

import { auth } from "@/auth";
import { NextResponse } from "next/server";
export default auth((req) => {
const isAuthenticated = !!req.auth;
const pathname = req.nextUrl.pathname;
const isLoginPage = pathname === "/login";
if (!isAuthenticated && !isLoginPage) {
const loginUrl = new URL("/login", req.url);
loginUrl.searchParams.set("callbackUrl", pathname);
return NextResponse.redirect(loginUrl);
}
if (isAuthenticated && isLoginPage) {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
});
export const config = {
matcher: [
"/((?!api/auth|_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};