generated from shadil-rayyan/nextjs_template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
20 lines (16 loc) · 604 Bytes
/
middleware.ts
File metadata and controls
20 lines (16 loc) · 604 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const token = request.cookies.get("__session")?.value;
// If no token and not already on /auth, redirect to login
if (!token && !request.nextUrl.pathname.startsWith("/auth")) {
const loginUrl = new URL("/auth", request.url);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
// Protect all routes except /auth and /_next
export const config = {
matcher: ["/((?!auth|_next|api|favicon.ico).*)"],
};