-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmiddleware.ts
89 lines (77 loc) · 2.35 KB
/
middleware.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { type NextRequest } from "next/server";
import { updateSession } from "@/utils/supabase/middleware";
import createMiddleware from "next-intl/middleware";
import { routing } from "./i18n/routing";
// API 路径前缀常量
const API_PREFIX = "/api";
// API 路径白名单
const API_WHITELIST = [
`${API_PREFIX}/webhook/stripe`,
`${API_PREFIX}/webhooktest`,
`${API_PREFIX}/auth/check-session`,
`${API_PREFIX}/create-checkout-session`,
`${API_PREFIX}/profile`,
];
export async function middleware(request: NextRequest) {
const { pathname, search } = request.nextUrl;
// 添加调试日志
console.log("Middleware processing path:", pathname);
// 1. Webhook 处理
if (pathname === `${API_PREFIX}/webhook/stripe`) {
console.log("Webhook request detected, bypassing all middleware");
return;
}
// 2. API 路由处理
if (pathname.startsWith(API_PREFIX)) {
if (API_WHITELIST.some((path) => pathname.startsWith(path))) {
console.log("Whitelisted API path:", pathname);
return;
}
return await updateSession(request);
}
// 3. 认证路由处理
if (pathname === "/auth/callback" || pathname.startsWith("/auth")) {
return await updateSession(request);
}
// 4. 多语言路由处理
const handleI18nRouting = createMiddleware({
...routing,
locales: routing.locales,
localePrefix: "always",
defaultLocale: "en",
localeDetection: true,
pathnames: {
"/": "/",
"/tags": "/tags",
"/blog": "/blog",
"/blog/:path*": "/blog/:path*",
"/docs": "/docs",
"/docs/:path*": "/docs/:path*",
"/payment/success": "/payment/success",
"/payment/cancel": "/payment/cancel",
"/api/webhooktest": "/api/webhooktest",
},
});
const response = handleI18nRouting(request);
// 设置中文本地化 cookie
if (pathname.startsWith("/zh")) {
response.headers.set("Set-Cookie", "NEXT_LOCALE=zh; Path=/");
}
return response;
}
// 更新 matcher 配置
export const config = {
matcher: [
// webhook 路径
"/api/webhook/stripe",
"/api/webhooktest",
// 其他匹配规则
"/((?!_next/static|_next/image|favicon.ico|.*\\..*).*)",
"/",
"/(en|zh)/:path*",
"/docs/:path*",
"/blog/:path*",
"/payment/:path*",
"/api/:path*",
],
};