-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathmiddleware.ts
More file actions
27 lines (23 loc) · 802 Bytes
/
middleware.ts
File metadata and controls
27 lines (23 loc) · 802 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
import { type NextRequest, NextResponse } from "next/server";
import { getAuthInstance as getAuth } from "@/modules/auth/utils/auth-utils";
export async function middleware(request: NextRequest) {
try {
// Validate session
const auth = await getAuth();
const session = await auth.api.getSession({
headers: request.headers,
});
if (!session) {
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
} catch (_error) {
// If session validation fails, redirect to login
return NextResponse.redirect(new URL("/login", request.url));
}
}
export const config = {
matcher: [
"/dashboard/:path*", // Protects /dashboard and all sub-routes
],
};