|
| 1 | +import { NextRequest, NextResponse } from 'next/server' |
| 2 | +import { PROTECTED_URLS } from '@/configs/urls' |
| 3 | +import { cookies } from 'next/headers' |
| 4 | +import { COOKIE_KEYS } from '@/configs/keys' |
| 5 | +import { supabaseAdmin } from '@/lib/clients/supabase/admin' |
| 6 | +import { createRouteClient } from '@/lib/clients/supabase/server' |
| 7 | + |
| 8 | +const TAB_URL_MAP: Record<string, (teamId: string) => string> = { |
| 9 | + sandboxes: (teamId) => PROTECTED_URLS.SANDBOXES(teamId), |
| 10 | + templates: (teamId) => PROTECTED_URLS.TEMPLATES(teamId), |
| 11 | + usage: (teamId) => PROTECTED_URLS.USAGE(teamId), |
| 12 | + billing: (teamId) => PROTECTED_URLS.BILLING(teamId), |
| 13 | + budget: (teamId) => PROTECTED_URLS.BUDGET(teamId), |
| 14 | + keys: (teamId) => PROTECTED_URLS.KEYS(teamId), |
| 15 | + team: (teamId) => PROTECTED_URLS.TEAM(teamId), |
| 16 | + account: (_) => PROTECTED_URLS.ACCOUNT_SETTINGS, |
| 17 | +} |
| 18 | + |
| 19 | +export async function GET(request: NextRequest) { |
| 20 | + // 1. Get the tab parameter |
| 21 | + const searchParams = request.nextUrl.searchParams |
| 22 | + const tab = searchParams.get('tab') |
| 23 | + |
| 24 | + if (!tab || !TAB_URL_MAP[tab]) { |
| 25 | + // Default to dashboard if no valid tab |
| 26 | + return NextResponse.redirect(new URL(PROTECTED_URLS.DASHBOARD, request.url)) |
| 27 | + } |
| 28 | + |
| 29 | + // 2. Create Supabase client and get user |
| 30 | + const supabase = createRouteClient(request) |
| 31 | + |
| 32 | + const { data, error } = await supabase.auth.getUser() |
| 33 | + |
| 34 | + if (error || !data.user) { |
| 35 | + // Redirect to sign-in if not authenticated |
| 36 | + return NextResponse.redirect(new URL('/sign-in', request.url)) |
| 37 | + } |
| 38 | + const cookieStore = await cookies() |
| 39 | + |
| 40 | + // 3. Resolve team ID (first try cookie, then fetch default) |
| 41 | + let teamId = cookieStore.get(COOKIE_KEYS.SELECTED_TEAM_ID)?.value |
| 42 | + let teamSlug = cookieStore.get(COOKIE_KEYS.SELECTED_TEAM_SLUG)?.value |
| 43 | + |
| 44 | + if (!teamId) { |
| 45 | + // No team in cookie, fetch user's default team |
| 46 | + const { data: teamsData } = await supabaseAdmin |
| 47 | + .from('users_teams') |
| 48 | + .select( |
| 49 | + ` |
| 50 | + team_id, |
| 51 | + is_default, |
| 52 | + team:teams(*) |
| 53 | + ` |
| 54 | + ) |
| 55 | + .eq('user_id', data.user.id) |
| 56 | + |
| 57 | + if (!teamsData?.length) { |
| 58 | + // No teams, redirect to new team creation |
| 59 | + return NextResponse.redirect( |
| 60 | + new URL(PROTECTED_URLS.NEW_TEAM, request.url) |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + // Use default team or first team |
| 65 | + const defaultTeam = teamsData.find((t) => t.is_default) || teamsData[0] |
| 66 | + teamId = defaultTeam.team_id |
| 67 | + teamSlug = defaultTeam.team?.slug || defaultTeam.team_id |
| 68 | + } |
| 69 | + |
| 70 | + // 4. Build the redirect URL using the tab mapping |
| 71 | + const urlGenerator = TAB_URL_MAP[tab] |
| 72 | + const redirectPath = urlGenerator(teamSlug || teamId) |
| 73 | + |
| 74 | + // 5. Redirect to the appropriate dashboard section |
| 75 | + return NextResponse.redirect(new URL(redirectPath, request.url)) |
| 76 | +} |
0 commit comments