-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
28 lines (22 loc) · 825 Bytes
/
middleware.ts
File metadata and controls
28 lines (22 loc) · 825 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// Define public routes that don't require authentication
const publicRoutes = ["/sign-in", "/sign-up", "/", "/about"];
export function middleware(request: NextRequest) {
const session = request.cookies.get("session");
const pathname = request.nextUrl.pathname;
// Allow public routes
if (publicRoutes.includes(pathname)) {
return NextResponse.next();
}
// Redirect to sign-in if no session exists
if (!session) {
const signInUrl = new URL("/sign-in", request.url);
signInUrl.searchParams.set("callbackUrl", pathname);
return NextResponse.redirect(signInUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|.*\\..*).*)"],
};