-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathmiddleware.ts
More file actions
259 lines (223 loc) · 6.95 KB
/
middleware.ts
File metadata and controls
259 lines (223 loc) · 6.95 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { checkUserTeamAuthorization, resolveTeamId } from '@/lib/utils/server'
import { kv } from '@/lib/clients/kv'
import { KV_KEYS } from '@/configs/keys'
import { NextRequest, NextResponse } from 'next/server'
import { COOKIE_KEYS } from '@/configs/keys'
import { AUTH_URLS, PROTECTED_URLS } from '@/configs/urls'
import { supabaseAdmin } from '@/lib/clients/supabase/admin'
import { z } from 'zod'
import { createServerClient } from '@supabase/ssr'
/**
* Core function to resolve team ID and ensure access for dashboard routes.
* Handles both direct team ID access and default team resolution.
*/
export async function resolveTeamForDashboard(
request: NextRequest,
userId: string
): Promise<{
teamId?: string
teamSlug?: string
redirect?: string
allowAccess?: boolean
}> {
// Check for tab query parameter - skip default redirects if present
const hasTabParam = request.nextUrl.searchParams.has('tab')
if (request.nextUrl.pathname === PROTECTED_URLS.NEW_TEAM) {
return { allowAccess: true }
}
const segments = request.nextUrl.pathname.split('/')
const teamIdOrSlug = segments.length > 2 ? segments[2] : null
const currentTeamId = request.cookies.get(COOKIE_KEYS.SELECTED_TEAM_ID)?.value
const currentTeamSlug = request.cookies.get(
COOKIE_KEYS.SELECTED_TEAM_SLUG
)?.value
if (teamIdOrSlug && teamIdOrSlug !== 'account') {
try {
const teamId = await resolveTeamId(teamIdOrSlug)
const hasAccess = await checkUserTeamAccess(userId, teamId)
if (!hasAccess) {
return { redirect: PROTECTED_URLS.DASHBOARD }
}
const isUuid = z.string().uuid().safeParse(teamIdOrSlug).success
const teamSlug = isUuid
? (await kv.get<string>(KV_KEYS.TEAM_ID_TO_SLUG(teamId))) || undefined
: teamIdOrSlug || undefined
return { teamId, teamSlug }
} catch (error) {
return { redirect: PROTECTED_URLS.DASHBOARD }
}
}
if (currentTeamId) {
const hasAccess = await checkUserTeamAccess(userId, currentTeamId)
if (hasAccess) {
const teamSlug =
currentTeamSlug ||
(await kv.get<string>(KV_KEYS.TEAM_ID_TO_SLUG(currentTeamId))) ||
undefined
// Skip redirect if we're at /dashboard with a tab parameter
if (
hasTabParam &&
request.nextUrl.pathname === PROTECTED_URLS.DASHBOARD
) {
return {
teamId: currentTeamId,
teamSlug,
// No redirect here - we'll let the page handle the tab parameter
// This case is handled by @/app/dashboard/route.ts
}
}
return {
teamId: currentTeamId,
teamSlug,
redirect:
teamIdOrSlug === 'account'
? undefined
: PROTECTED_URLS.SANDBOXES(teamSlug || currentTeamId),
}
}
}
const { data: teamsData, error: teamsError } = await supabaseAdmin
.from('users_teams')
.select(
`
team_id,
is_default,
team:teams(*)
`
)
.eq('user_id', userId)
if (teamsError) {
return { redirect: '/' }
}
if (!teamsData?.length) {
return {
redirect: PROTECTED_URLS.NEW_TEAM,
}
}
const defaultTeam = teamsData.find((t) => t.is_default) || teamsData[0]!
// Skip redirect if we're at /dashboard with a tab parameter
if (hasTabParam && request.nextUrl.pathname === PROTECTED_URLS.DASHBOARD) {
return {
teamId: defaultTeam.team_id,
teamSlug: defaultTeam.team?.slug || undefined,
// No redirect here - we'll let the page handle the tab parameter
}
}
return {
teamId: defaultTeam.team_id,
teamSlug: defaultTeam.team?.slug || undefined,
redirect:
teamIdOrSlug === 'account'
? undefined
: PROTECTED_URLS.SANDBOXES(
defaultTeam.team?.slug || defaultTeam.team_id
),
}
}
/**
* Checks user access to team with caching
*/
export async function checkUserTeamAccess(
userId: string,
teamId: string
): Promise<boolean> {
const cacheKey = KV_KEYS.USER_TEAM_ACCESS(userId, teamId)
const cached = await kv.get<boolean>(cacheKey)
if (cached !== null) {
return cached
}
const hasAccess = await checkUserTeamAuthorization(userId, teamId)
await kv.set(cacheKey, hasAccess, { ex: 60 * 60 }) // 1 hour
return hasAccess
}
// URL utility functions
export function isAuthRoute(pathname: string): boolean {
return (
pathname.includes(AUTH_URLS.SIGN_IN) ||
pathname.includes(AUTH_URLS.SIGN_UP) ||
pathname.includes(AUTH_URLS.FORGOT_PASSWORD)
)
}
export function isDashboardRoute(pathname: string): boolean {
return pathname.startsWith(PROTECTED_URLS.DASHBOARD)
}
export function buildRedirectUrl(path: string, request: NextRequest): URL {
return new URL(path, request.url)
}
// Authentication utility functions
export async function getUserSession(
supabase: ReturnType<typeof createServerClient>
) {
return await supabase.auth.getUser()
}
export function getAuthRedirect(
request: NextRequest,
isAuthenticated: boolean
): NextResponse | null {
if (isDashboardRoute(request.nextUrl.pathname) && !isAuthenticated) {
return NextResponse.redirect(buildRedirectUrl(AUTH_URLS.SIGN_IN, request))
}
if (isAuthRoute(request.nextUrl.pathname) && isAuthenticated) {
return NextResponse.redirect(
buildRedirectUrl(PROTECTED_URLS.DASHBOARD, request)
)
}
return null
}
const COOKIE_OPTIONS = {
maxAge: 60 * 60 * 24 * 30, // 30 days
path: '/',
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax' as const,
}
// Cookie management functions
export function setCookies(
response: NextResponse,
teamId: string,
teamSlug?: string
): NextResponse {
response.cookies.set(COOKIE_KEYS.SELECTED_TEAM_ID, teamId, COOKIE_OPTIONS)
if (teamSlug) {
response.cookies.set(
COOKIE_KEYS.SELECTED_TEAM_SLUG,
teamSlug,
COOKIE_OPTIONS
)
}
return response
}
export function clearTeamCookies(response: NextResponse): NextResponse {
response.cookies.delete(COOKIE_KEYS.SELECTED_TEAM_ID)
response.cookies.delete(COOKIE_KEYS.SELECTED_TEAM_SLUG)
return response
}
// Team resolution handler
export function handleTeamResolution(
request: NextRequest,
response: NextResponse,
teamResult: Awaited<ReturnType<typeof resolveTeamForDashboard>>
): NextResponse {
const { teamId, teamSlug, redirect, allowAccess } = teamResult
// Allow special routes to bypass team checks
if (allowAccess) {
return response
}
if (!teamId) {
// No valid team access, redirect to dashboard
const redirectResponse = NextResponse.redirect(
buildRedirectUrl(redirect || PROTECTED_URLS.DASHBOARD, request),
{ status: 302 }
)
return clearTeamCookies(redirectResponse)
}
// Handle redirects and set cookie
if (redirect) {
const redirectResponse = NextResponse.redirect(
buildRedirectUrl(redirect, request),
{ status: 302 }
)
return setCookies(redirectResponse, teamId, teamSlug)
}
// Continue with request, ensuring cookies are set
return setCookies(response, teamId, teamSlug)
}