-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
39 lines (33 loc) · 1.55 KB
/
Copy pathmiddleware.ts
File metadata and controls
39 lines (33 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { getToken } from "next-auth/jwt";
import { NextRequest, NextResponse } from "next/server";
// Gate the API and authenticated pages behind a valid NextAuth session.
// Without this, /api/* (e.g. GET /api/settings) was reachable unauthenticated.
//
// IMPORTANT exclusions (must stay public, or sign-in breaks):
// /api/auth/* — NextAuth endpoints
// /api/plex-auth/* — the Plex PIN/OAuth login flow (used while logged OUT)
const PUBLIC_API_PREFIXES = ["/api/auth", "/api/plex-auth"];
const PROTECTED_PAGES = ["/settings", "/users", "/rules", "/activity", "/library-access"];
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const token = await getToken({ req: request, secret: process.env.NEXTAUTH_SECRET });
// Protect API routes (except the public auth/login-flow ones)
if (pathname.startsWith("/api/")) {
const isPublic = PUBLIC_API_PREFIXES.some((p) => pathname.startsWith(p));
if (!isPublic && !token) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
return NextResponse.next();
}
// Protect authenticated pages — redirect to /login when signed out
if (PROTECTED_PAGES.some((p) => pathname === p || pathname.startsWith(p + "/"))) {
if (!token) {
const loginUrl = new URL("/login", request.url);
return NextResponse.redirect(loginUrl);
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/api/:path*", "/settings/:path*", "/users/:path*", "/rules/:path*", "/activity/:path*", "/library-access/:path*"],
};