-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
32 lines (27 loc) · 857 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
32 lines (27 loc) · 857 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
31
32
import { auth } from "@/lib/auth";
import { NextResponse } from "next/server";
export default auth((req) => {
const { pathname } = req.nextUrl;
// Allow public routes
if (pathname === "/" || pathname.startsWith("/login") || pathname.startsWith("/api/auth")) {
return NextResponse.next();
}
// Redirect to login if not authenticated
if (!req.auth) {
const loginUrl = new URL("/login", req.url);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
});
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api/auth (authentication routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!api/auth|_next/static|_next/image|favicon.ico).*)",
],
};