MCPcopy Create free account
hub / github.com/Fibi66/FeiControl / middleware

Function middleware

src/middleware.ts:15–45  ·  view source on GitHub ↗
(request: NextRequest)

Source from the content-addressed store, hash-verified

13}
14
15export function middleware(request: NextRequest) {
16 const { pathname } = request.nextUrl;
17
18 // Always allow public pages (login)
19 if (PUBLIC_ROUTES.has(pathname)) {
20 return NextResponse.next();
21 }
22
23 // Always allow public API routes (auth + health)
24 if (PUBLIC_API_PREFIXES.some((prefix) => pathname.startsWith(prefix))) {
25 return NextResponse.next();
26 }
27
28 // Check authentication
29 if (!isAuthenticated(request)) {
30 // For API routes: return 401 JSON (not a redirect)
31 if (pathname.startsWith("/api/")) {
32 return NextResponse.json(
33 { error: "Unauthorized", message: "Authentication required" },
34 { status: 401 }
35 );
36 }
37
38 // For page routes: redirect to login
39 const loginUrl = new URL("/login", request.url);
40 loginUrl.searchParams.set("from", pathname);
41 return NextResponse.redirect(loginUrl);
42 }
43
44 return NextResponse.next();
45}
46
47export const config = {
48 matcher: [

Callers

nothing calls this directly

Calls 1

isAuthenticatedFunction · 0.70

Tested by

no test coverage detected