(request: NextRequest)
| 73 | const handleI18nRouting = createMiddleware(routing) |
| 74 | |
| 75 | export async function middleware(request: NextRequest) { |
| 76 | const pathname = request.nextUrl.pathname |
| 77 | const pathnameWithoutLocale = getPathnameWithoutLocale(pathname) |
| 78 | |
| 79 | // API路由跳过国际化处理,直接处理认证 |
| 80 | if (pathname.startsWith("/api/")) { |
| 81 | const { response: authResponse, user } = await updateSession(request, NextResponse.next()) |
| 82 | |
| 83 | // 检查是否需要认证 |
| 84 | const isPublicAPIRoute = isRouteMatch(pathnameWithoutLocale, routeConfig.publicAPIRoutes) |
| 85 | |
| 86 | if (!isPublicAPIRoute && !user) { |
| 87 | return handleUnauthenticated(request, pathnameWithoutLocale) |
| 88 | } |
| 89 | |
| 90 | return authResponse |
| 91 | } |
| 92 | |
| 93 | const i18nResponse = handleI18nRouting(request) |
| 94 | |
| 95 | // 然后处理认证 |
| 96 | const { response, user } = await updateSession(request, i18nResponse) |
| 97 | |
| 98 | const isProtectedRoute = isRouteMatch(pathnameWithoutLocale, routeConfig.protectedRoutes) |
| 99 | |
| 100 | if (isProtectedRoute && !user) { |
| 101 | return handleUnauthenticated(request, pathnameWithoutLocale) |
| 102 | } |
| 103 | |
| 104 | return response |
| 105 | } |
| 106 | |
| 107 | export const config = { |
| 108 | matcher: [ |
nothing calls this directly
no test coverage detected