-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
60 lines (54 loc) · 1.84 KB
/
middleware.ts
File metadata and controls
60 lines (54 loc) · 1.84 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import jwt from "jsonwebtoken";
export const config = {
matcher: [
"/account/:path*",
"/dashboard/:path*",
"/api/protected/:path*",
// Если хочешь — добавь ещё пути, которые должны требовать авторизацию
],
runtime: "nodejs",
};
export function middleware(req: NextRequest) {
const token = req.cookies.get("token")?.value;
const hasAccepted = req.cookies.get("agreementAccepted")?.value === "true";
const isApi = req.nextUrl.pathname.startsWith("/api/");
const isAgreementPage = req.nextUrl.pathname.startsWith("/agreement");
const isSignin = req.nextUrl.pathname.startsWith("/signin");
// 1️⃣ Проверяем — если пользователь не принял соглашение
if (!hasAccepted && !isAgreementPage) {
const url = req.nextUrl.clone();
url.pathname = "/agreement";
return NextResponse.redirect(url);
}
// 2️⃣ Если токена нет
if (!token) {
if (isApi) {
return new NextResponse(JSON.stringify({ ok: false, error: "No token" }), {
status: 401,
headers: { "Content-Type": "application/json" },
});
}
if (!isSignin) {
const url = req.nextUrl.clone();
url.pathname = "/signin";
return NextResponse.redirect(url);
}
}
// 3️⃣ Проверяем JWT
try {
if (token) jwt.verify(token, process.env.JWT_SECRET!);
return NextResponse.next();
} catch {
if (isApi) {
return new NextResponse(JSON.stringify({ ok: false, error: "Invalid token" }), {
status: 401,
headers: { "Content-Type": "application/json" },
});
}
const url = req.nextUrl.clone();
url.pathname = "/signin";
return NextResponse.redirect(url);
}
}