-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmiddleware.ts
102 lines (84 loc) · 2.89 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
90
91
92
93
94
95
96
97
98
99
100
101
import { locales } from "@/lib/i18n";
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
const isProtectedRoute = createRouteMatcher([
"/dashboard(.*)",
"/forum(.*)",
"/workbench(.*)",
]);
const isApiRoute = createRouteMatcher(["/api(.*)"]);
const PUBLIC_FILE = /\.(.*)$/;
const notNeedI18NRoute = createRouteMatcher(["/sign-in(.*)", "/sign-up(.*)"]);
const needI18NRouteRedirect = createRouteMatcher([
"/((?!api|_next/static|_next/image|.*\\.(?:ico|png|jpg|jpeg|svg|gif|webp|js|css|woff|woff2|ttf|eot)).*)",
]);
// 如果路径不匹配,内部重定向到 404 页面
const matchedRoute = createRouteMatcher([
"/dashboard(.*)",
"/forum(.*)",
"/workbench(.*)",
"/sign-in(.*)",
"/sign-up(.*)",
"/_next(.*)",
"/api(.*)",
"/((?!.*\\.(?:ico|png|svg|jpg|jpeg|xml|txt)$).*)",
]);
export default clerkMiddleware((auth, req) => {
console.log("req.url: ", req.url);
// auth
if (isProtectedRoute(req)) {
console.log("auth protect!!!");
auth().protect();
}
if (isApiRoute(req)) {
const authObject = auth();
if (!authObject.userId) {
return new NextResponse("Unauthorized, please sign in.", { status: 401 });
}
}
console.log("auth pass!!");
if (
req.nextUrl.pathname.startsWith("/_next") ||
req.nextUrl.pathname.includes("/api/") ||
PUBLIC_FILE.test(req.nextUrl.pathname)
) {
console.log("return!");
return;
}
console.log("needI18NRouteRedirect pass!!");
const { pathname } = req.nextUrl;
const defaultLocale = "en"; // 这里可以根据需要设置默认的 locale
// 检查路径是否包含 locale 前缀
const locale = locales.find(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
);
// 如果路径是 `/en/{任意page}`,则重定向到 `/{任意page}`
if (pathname.startsWith(`/${defaultLocale}/`)) {
const newPathname = pathname.replace(`/${defaultLocale}/`, "/");
return NextResponse.redirect(new URL(newPathname, req.url));
}
// 如果路径不包含 locale 前缀,则内部重定向到 `/en/{非locale page}`
if (!locale) {
console.log("does not contain locale, rewrite to /en: ", pathname);
req.nextUrl.pathname = `/${defaultLocale}${pathname}`;
return NextResponse.rewrite(req.nextUrl);
}
// 其他逻辑:根路径重定向等
if (pathname === "/") {
req.nextUrl.pathname = `/${defaultLocale}`;
return NextResponse.rewrite(req.nextUrl);
}
if (locale === defaultLocale && needI18NRouteRedirect(req)) {
req.nextUrl.pathname = `/${locale}${req.nextUrl.pathname}`;
return NextResponse.rewrite(req.nextUrl);
}
if (!matchedRoute(req)) {
req.nextUrl.pathname = "/404";
return NextResponse.rewrite(req.nextUrl);
}
});
export const config = {
matcher: [
"/((?!_next/static|_next/image|.*\\.(?:ico|png|jpg|jpeg|svg|gif|webp|js|css|woff|woff2|ttf|eot)).*)",
],
};