MCPcopy Create free account
hub / github.com/context-space/context-space / middleware

Function middleware

web/src/middleware.ts:75–105  ·  view source on GitHub ↗
(request: NextRequest)

Source from the content-addressed store, hash-verified

73const handleI18nRouting = createMiddleware(routing)
74
75export 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
107export const config = {
108 matcher: [

Callers

nothing calls this directly

Calls 4

updateSessionFunction · 0.90
getPathnameWithoutLocaleFunction · 0.85
isRouteMatchFunction · 0.85
handleUnauthenticatedFunction · 0.85

Tested by

no test coverage detected