-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
29 lines (24 loc) · 928 Bytes
/
middleware.ts
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
import { NextRequest, NextResponse } from "next/server";
import { cookies } from "next/headers";
// Middleware function
export async function middleware(req: NextRequest) {
const token = cookies().get("auth-token");
// Declare public and protected routes
const protectedRoutes = ["/"];
// Determine if current route is protected or public
const path = req.nextUrl.pathname;
const isProtectedRoute = protectedRoutes.some((route) =>
path.startsWith(route)
);
// Redirect if user is not authenticated
if (isProtectedRoute && !token && path !== "/login") {
return NextResponse.redirect(new URL("/login", req.nextUrl));
}
return NextResponse.next();
}
export const config = {
matcher: [
// Apply middleware to all paths except those containing '/api', the landing page '/', and other specified ones
"/((?!api|/api|_next/static|_next/image|favicon.ico|.*\\.png$|.*\\.jpg$|^$).*)",
],
};