25 lines
832 B
TypeScript
25 lines
832 B
TypeScript
|
|
import { NextResponse, type NextRequest } from "next/server";
|
||
|
|
import { getToken } from "next-auth/jwt";
|
||
|
|
|
||
|
|
const PUBLIC_PATHS = ["/login", "/setup", "/api/auth", "/api/setup", "/api/v1"];
|
||
|
|
|
||
|
|
export async function middleware(req: NextRequest) {
|
||
|
|
const { pathname } = req.nextUrl;
|
||
|
|
if (PUBLIC_PATHS.some((p) => pathname === p || pathname.startsWith(p + "/") || pathname.startsWith(p))) {
|
||
|
|
return NextResponse.next();
|
||
|
|
}
|
||
|
|
|
||
|
|
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET || "nexredirect-dev-secret-please-change" });
|
||
|
|
if (!token) {
|
||
|
|
const url = req.nextUrl.clone();
|
||
|
|
url.pathname = "/login";
|
||
|
|
url.searchParams.set("from", pathname);
|
||
|
|
return NextResponse.redirect(url);
|
||
|
|
}
|
||
|
|
return NextResponse.next();
|
||
|
|
}
|
||
|
|
|
||
|
|
export const config = {
|
||
|
|
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
|
||
|
|
};
|