-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
33 lines (28 loc) · 825 Bytes
/
middleware.ts
File metadata and controls
33 lines (28 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
29
30
31
32
33
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
// Allow public assets and auth pages
if (
pathname.startsWith("/_next") ||
pathname.startsWith("/api") ||
pathname === "/favicon.ico" ||
pathname === "/login" ||
pathname === "/register"
) {
return NextResponse.next();
}
if (pathname.startsWith("/dashboard")) {
const token = req.cookies.get("token")?.value;
if (!token) {
const url = req.nextUrl.clone();
url.pathname = "/login";
url.search = `redirectTo=${encodeURIComponent(pathname)}`;
return NextResponse.redirect(url);
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*"],
};