-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
26 lines (22 loc) · 1020 Bytes
/
middleware.ts
File metadata and controls
26 lines (22 loc) · 1020 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export async function middleware(request: NextRequest) {
// NextAuth v4 использует следующие имена cookies:
// - next-auth.session-token (для HTTP)
// - __Secure-next-auth.session-token (для HTTPS)
// - __Host-next-auth.session-token (для HTTPS с определенными настройками)
const sessionToken =
request.cookies.get("next-auth.session-token")?.value ||
request.cookies.get("__Secure-next-auth.session-token")?.value ||
request.cookies.get("__Host-next-auth.session-token")?.value;
// Если нет токена сессии, редиректим на /login
if (!sessionToken) {
const url = new URL("/login", request.url);
url.searchParams.set("callbackUrl", request.url);
return NextResponse.redirect(url);
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*", "/my-prompts/:path*"],
};