-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathmiddleware.ts
More file actions
119 lines (104 loc) · 3.35 KB
/
Copy pathmiddleware.ts
File metadata and controls
119 lines (104 loc) · 3.35 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { NextResponse, type NextRequest } from 'next/server'
import { createServerClient } from '@supabase/ssr'
import {
getAuthRedirect,
getUserSession,
handleTeamResolution,
isAuthRoute,
isDashboardRoute,
resolveTeamForDashboard,
} from './server/middleware'
import { PROTECTED_URLS } from './configs/urls'
import { logError } from './lib/clients/logger'
import { ERROR_CODES } from './configs/logs'
import { getRewriteForPath } from './lib/utils/rewrites'
import { ALLOW_SEO_INDEXING } from './configs/flags'
export async function middleware(request: NextRequest) {
try {
// Catch-all route rewrite paths should not be handled by middleware
// NOTE: We don't handle this via config matchers, because nextjs configs need to be static
const { config: routeRewriteConfig } = getRewriteForPath(
request.nextUrl.pathname,
'route'
)
if (routeRewriteConfig) {
return NextResponse.next({
request,
})
}
// Check if the path should be rewritten by middleware
const { config: middlewareRewriteConfig } = getRewriteForPath(
request.nextUrl.pathname,
'middleware'
)
if (middlewareRewriteConfig) {
const rewriteUrl = new URL(request.url)
rewriteUrl.hostname = middlewareRewriteConfig.domain
rewriteUrl.protocol = 'https'
rewriteUrl.port = ''
const headers = new Headers(request.headers)
if (ALLOW_SEO_INDEXING) {
headers.set('x-e2b-should-index', '1')
}
return NextResponse.rewrite(rewriteUrl, {
request: {
headers,
},
})
}
// Setup response and Supabase client
const response = NextResponse.next({
request,
})
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll() {
return request.cookies.getAll()
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) => {
response.cookies.set(name, value, options)
})
},
},
}
)
const { error, data } = await getUserSession(supabase)
// Handle authentication redirects
const authRedirect = getAuthRedirect(request, !error)
if (authRedirect) return authRedirect
// Early return for non-dashboard routes or no user
if (!data?.user || !isDashboardRoute(request.nextUrl.pathname)) {
return response
}
// Handle team resolution for all dashboard routes
const teamResult = await resolveTeamForDashboard(request, data.user.id)
// Process team resolution result
return handleTeamResolution(request, response, teamResult)
} catch (error) {
logError(ERROR_CODES.MIDDLEWARE, error)
// Return a basic response to avoid infinite loops
return NextResponse.next({
request,
})
}
}
export const config = {
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - images - .svg, .png, .jpg, .jpeg, .gif, .webp
* - api routes
* - vercel analytics route
* - sentry routes
* - posthog routes
*/
'/((?!_next/static|_next/image|favicon.ico|api/|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$|_vercel/|monitoring|ingest/).*)',
],
}