-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
56 lines (46 loc) · 1.73 KB
/
proxy.ts
File metadata and controls
56 lines (46 loc) · 1.73 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
import createMiddleware from "next-intl/middleware";
import { NextRequest, NextResponse } from "next/server";
import { routing } from "@/i18n/routing";
const intl = createMiddleware(routing);
export default function middleware(req: NextRequest) {
const intlRes = intl(req);
const { pathname } = req.nextUrl;
const segments = pathname.split("/").filter(Boolean);
const locale = segments[0] || routing.defaultLocale;
const second = segments[1] ?? "";
const isLoginPath = second === "login";
const isInvitePath = second === "invite";
const refreshToken = req.cookies.get("refresh_token")?.value;
const isAuthenticated = Boolean(refreshToken);
// Добавляем pathname в headers для использования в Server Components
const requestHeaders = new Headers(req.headers);
requestHeaders.set("x-pathname", pathname);
if (!isAuthenticated && !isLoginPath && !isInvitePath) {
const url = req.nextUrl.clone();
url.pathname = `/${locale}/login`;
const response = NextResponse.redirect(url);
response.headers.set("x-pathname", pathname);
return response;
}
if (isAuthenticated && isLoginPath) {
const url = req.nextUrl.clone();
url.pathname = `/${locale}`;
const response = NextResponse.redirect(url);
response.headers.set("x-pathname", pathname);
return response;
}
// Если используем intl response, добавляем header к нему
if (intlRes) {
intlRes.headers.set("x-pathname", pathname);
return intlRes;
}
// Иначе создаём новый response с headers
return NextResponse.next({
request: {
headers: requestHeaders,
},
});
}
export const config = {
matcher: "/((?!api|trpc|_next|_vercel|.*\\..*).*)",
};