-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
44 lines (30 loc) · 1.05 KB
/
middleware.ts
File metadata and controls
44 lines (30 loc) · 1.05 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
// middleware.ts
/*
🌐 Middleware in Next.js (App Router)
🔹 Purpose:
- Middleware runs **before** a request is completed.
- Useful for authentication, redirects, logging, header injection, etc.
🔹 File Location:
- Place a `middleware.ts` file at the root or in any nested folder under `/src`.
🔹 Syntax:
- Exports a function that receives a `NextRequest` object.
- Use `NextResponse` to respond or redirect.
🔹 Example:
*/
// ---------------CODE-START--------------- //
// import { NextResponse } from "next/server";
// import type { NextRequest } from "next/server";
// export function middleware(request: NextRequest) {
// const isLoggedIn = false; // Example logic
// if (!isLoggedIn && request.nextUrl.pathname.startsWith("/dashboard")) {
// return NextResponse.redirect(new URL("/login", request.url));
// }
// return NextResponse.next();
// }
// ---------------CODE-END--------------- //
/*
🔸 Configure paths in `middleware.ts` using `matcher` in `next.config.js`:
export const config = {
matcher: ["/dashboard/:path*"],
};
*/