-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
28 lines (24 loc) · 766 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
28 lines (24 loc) · 766 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const sessionToken = request.cookies.get("session_token")?.value;
if (
request.nextUrl.pathname.startsWith("/dashboard") ||
request.nextUrl.pathname.startsWith("/contact") ||
request.nextUrl.pathname.startsWith("/profile")
) {
if (!sessionToken) {
const redirectUrl = new URL("/api/auth/status", request.url);
redirectUrl.searchParams.set("redirect", request.nextUrl.pathname);
return NextResponse.redirect(redirectUrl);
}
}
return NextResponse.next();
}
export const config = {
matcher: [
"/dashboard/:path*",
"/profile/:path*",
"/contact/:path*"
],
};