17 lines
802 B
TypeScript
17 lines
802 B
TypeScript
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
import { verifySession, SESSION_COOKIE } from "./lib/session";
|
|
|
|
const PUBLIC_PREFIXES = ["/login", "/api/auth", "/api/health", "/accept-invite", "/api/users/accept"];
|
|
|
|
export async function middleware(req: NextRequest) {
|
|
const p = req.nextUrl.pathname;
|
|
if (PUBLIC_PREFIXES.some((prefix) => p === prefix || p.startsWith(`${prefix}/`))) {
|
|
return NextResponse.next();
|
|
}
|
|
const token = req.cookies.get(SESSION_COOKIE)?.value;
|
|
if (token && (await verifySession(token))) return NextResponse.next();
|
|
return NextResponse.redirect(new URL("/login", req.url));
|
|
}
|
|
export const config = { matcher: ["/((?!_next/static|_next/image|favicon.ico|icon.png|apple-icon.png|manifest.webmanifest|robots.txt).*)"] };
|