-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
35 lines (28 loc) · 964 Bytes
/
middleware.ts
File metadata and controls
35 lines (28 loc) · 964 Bytes
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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const PUBLIC_PATHS = ["/login", "/legal", "/privacy", "/api/auth/"];
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Skip API routes, Next.js internals, and static assets
if (
pathname.startsWith("/api/") ||
pathname.startsWith("/_next/") ||
pathname.startsWith("/static/") ||
pathname.includes(".")
) {
return NextResponse.next();
}
// Skip public pages
if (PUBLIC_PATHS.some(path => pathname.startsWith(path))) {
return NextResponse.next();
}
// Redirect to login if session cookie is absent
const sessionCookie = request.cookies.get("code-scrobble-session");
if (!sessionCookie) {
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};