-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
45 lines (38 loc) · 1.23 KB
/
proxy.ts
File metadata and controls
45 lines (38 loc) · 1.23 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
40
41
42
43
44
45
import { auth } from "@/auth";
import { NextResponse, type NextRequest } from "next/server";
// Centralized route protection for authenticated sections.
type AuthRequest = NextRequest & { auth?: unknown };
export default auth((req: AuthRequest) => {
const isAuthenticated = !!req.auth;
const { pathname } = req.nextUrl;
// Protected routes - require authentication
const protectedPaths = [
"/dashboard",
"/editor",
"/settings",
];
// Check if current path is protected
const isProtectedPath = protectedPaths.some((path) =>
pathname.startsWith(path)
);
// Redirect to home if accessing protected route without auth
if (isProtectedPath && !isAuthenticated) {
const url = new URL("/", req.url);
return NextResponse.redirect(url);
}
return NextResponse.next();
});
// Configure which routes this middleware runs on
export const config = {
// Run on all routes except static files and API routes
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder files
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};