generated from CodeGuide-dev/codeguide-starter-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
30 lines (25 loc) · 893 Bytes
/
middleware.ts
File metadata and controls
30 lines (25 loc) · 893 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
import { NextRequest, NextResponse } from "next/server";
import { headers } from "next/headers";
import { auth } from "@/lib/auth";
export async function middleware(request: NextRequest) {
// Skip authentication for static files, API routes, and auth pages
if (
request.nextUrl.pathname.startsWith('/_next') ||
request.nextUrl.pathname.startsWith('/api') ||
request.nextUrl.pathname.startsWith('/sign-in') ||
request.nextUrl.pathname.startsWith('/sign-up')
) {
return NextResponse.next();
}
const session = await auth.api.getSession({
headers: await headers()
})
if (!session) {
return NextResponse.redirect(new URL("/sign-in", request.url));
}
return NextResponse.next();
}
export const config = {
runtime: "nodejs",
matcher: ["/dashboard/:path*"], // Apply middleware to dashboard routes
};