Skip to content

Commit 94193ff

Browse files
committed
fix: pass sandbox management auth from server
1 parent d0aea9a commit 94193ff

14 files changed

Lines changed: 113 additions & 77 deletions

File tree

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
import { cookies } from 'next/headers'
2+
import { redirect } from 'next/navigation'
23
import { COOKIE_KEYS } from '@/configs/cookies'
4+
import { AUTH_URLS, PROTECTED_URLS } from '@/configs/urls'
5+
import { auth } from '@/core/server/auth'
6+
import { getTeamIdFromSlug } from '@/core/server/functions/team/get-team-id-from-slug'
7+
import { createSandboxManagementAuth } from '@/core/shared/sandbox-management-auth.server'
38
import SandboxInspectView from '@/features/dashboard/sandbox/inspect/view'
49

510
const DEFAULT_ROOT_PATH = '/home/user'
611

7-
export default async function SandboxInspectPage() {
8-
const cookieStore = await cookies()
12+
interface SandboxInspectPageProps {
13+
params: Promise<{
14+
teamSlug: string
15+
}>
16+
}
17+
18+
export default async function SandboxInspectPage({
19+
params,
20+
}: SandboxInspectPageProps) {
21+
const [{ teamSlug }, cookieStore, authContext] = await Promise.all([
22+
params,
23+
cookies(),
24+
auth.getAuthContext(),
25+
])
26+
27+
if (!authContext) {
28+
redirect(AUTH_URLS.SIGN_IN)
29+
}
30+
31+
const teamId = await getTeamIdFromSlug(teamSlug, authContext.accessToken)
32+
if (!teamId.ok || !teamId.data) {
33+
redirect(PROTECTED_URLS.DASHBOARD)
34+
}
35+
936
const rootPath =
1037
cookieStore.get(COOKIE_KEYS.SANDBOX_INSPECT_ROOT_PATH)?.value ||
1138
DEFAULT_ROOT_PATH
1239

13-
return <SandboxInspectView rootPath={rootPath} />
40+
return (
41+
<SandboxInspectView
42+
rootPath={rootPath}
43+
sandboxManagementAuth={createSandboxManagementAuth(
44+
authContext,
45+
teamId.data
46+
)}
47+
/>
48+
)
1449
}

src/app/dashboard/[teamSlug]/team-gate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useQuery } from '@tanstack/react-query'
44
import { DASHBOARD_TEAMS_LIST_QUERY_OPTIONS } from '@/core/application/teams/queries'
55
import { DASHBOARD_USER_PROFILE_QUERY_OPTIONS } from '@/core/application/user/queries'
6-
import type { AuthUser } from '@/core/server/auth'
6+
import type { AuthUser } from '@/core/shared/auth-user'
77
import { DashboardContextProvider } from '@/features/dashboard/context'
88
import LoadingLayout from '@/features/dashboard/loading-layout'
99
import { useTRPC } from '@/trpc/client'

src/app/dashboard/terminal/page.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { auth } from '@/core/server/auth'
1212
import { resolveUserTeam } from '@/core/server/functions/team/resolve-user-team'
1313
import { infra } from '@/core/shared/clients/api'
14+
import { createSandboxManagementAuth } from '@/core/shared/sandbox-management-auth.server'
1415
import { SandboxIdSchema } from '@/core/shared/schemas/api'
1516
import DashboardTerminal from '@/features/dashboard/terminal/dashboard-terminal'
1617
import { normalizeTerminalTemplate } from '@/features/dashboard/terminal/template'
@@ -110,7 +111,10 @@ export default async function TerminalPage({
110111
initialCommand={command}
111112
initialSandboxId={terminalSandboxId}
112113
initialTemplate={terminalTemplate}
113-
teamId={team.id}
114+
sandboxManagementAuth={createSandboxManagementAuth(
115+
authContext,
116+
team.id
117+
)}
114118
/>
115119
</main>
116120
)

src/core/server/auth/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ export function createAuthForHeaders(headers: Headers): AuthProvider {
4040
: createSupabaseAuthForHeaders(headers)
4141
}
4242

43+
export type { AuthUser } from '@/core/shared/auth-user'
4344
export type { AuthAdmin } from './admin'
44-
export type { AuthUser } from './types'

src/core/server/auth/types.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
export type AuthUser = {
2-
id: string
3-
email: string | null
4-
name: string | null
5-
avatarUrl: string | null
6-
providers: string[]
7-
canChangeEmail: boolean
8-
canChangePassword: boolean
9-
}
1+
import type { AuthUser } from '@/core/shared/auth-user'
2+
3+
export type { AuthUser } from '@/core/shared/auth-user'
104

115
export type AuthContext = {
126
user: AuthUser

src/core/shared/auth-user.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type AuthUser = {
2+
id: string
3+
email: string | null
4+
name: string | null
5+
avatarUrl: string | null
6+
providers: string[]
7+
canChangeEmail: boolean
8+
canChangePassword: boolean
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'server-only'
2+
3+
import { SUPABASE_AUTH_HEADERS } from '@/configs/api'
4+
import type { AuthContext } from '@/core/server/auth/types'
5+
import type { SandboxManagementAuth } from './sandbox-management-auth'
6+
7+
export function createSandboxManagementAuth(
8+
authContext: AuthContext,
9+
teamId: string
10+
): SandboxManagementAuth {
11+
return {
12+
headers: SUPABASE_AUTH_HEADERS(authContext.accessToken, teamId),
13+
userId: authContext.user.id,
14+
}
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface SandboxManagementAuth {
2+
userId: string
3+
headers: Record<string, string>
4+
}

src/features/dashboard/context.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { createContext, type ReactNode, useContext, useEffect } from 'react'
44
import { useDebounceCallback } from 'usehooks-ts'
55
import type { TeamModel } from '@/core/modules/teams/models'
6-
import type { AuthUser } from '@/core/server/auth'
6+
import type { AuthUser } from '@/core/shared/auth-user'
77

88
interface DashboardContextValue {
99
team: TeamModel

src/features/dashboard/sandbox/inspect/context.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use client'
22

33
import Sandbox from 'e2b'
4-
import { useRouter } from 'next/navigation'
54
import type { ReactNode } from 'react'
65
import {
76
createContext,
@@ -11,9 +10,7 @@ import {
1110
useMemo,
1211
useRef,
1312
} from 'react'
14-
import { SUPABASE_AUTH_HEADERS } from '@/configs/api'
15-
import { AUTH_URLS } from '@/configs/urls'
16-
import { supabase } from '@/core/shared/clients/supabase/client'
13+
import type { SandboxManagementAuth } from '@/core/shared/sandbox-management-auth'
1714
import { useSandboxInspectAnalytics } from '@/lib/hooks/use-analytics'
1815
import { getParentPath, normalizePath } from '@/lib/utils/filesystem'
1916
import { useDashboard } from '../../context'
@@ -34,11 +31,13 @@ const SandboxInspectContext = createContext<SandboxInspectContextValue | null>(
3431
interface SandboxInspectProviderProps {
3532
children: ReactNode
3633
rootPath: string
34+
sandboxManagementAuth: SandboxManagementAuth
3735
}
3836

3937
export default function SandboxInspectProvider({
4038
children,
4139
rootPath,
40+
sandboxManagementAuth,
4241
}: SandboxInspectProviderProps) {
4342
const { team } = useDashboard()
4443
const teamId = team.id
@@ -47,7 +46,6 @@ export default function SandboxInspectProvider({
4746
const storeRef = useRef<FilesystemStore | null>(null)
4847
const sandboxManagerRef = useRef<SandboxManager | null>(null)
4948

50-
const router = useRouter()
5149
const { trackInteraction } = useSandboxInspectAnalytics()
5250

5351
// ---------- synchronous store initialisation ----------
@@ -181,19 +179,12 @@ export default function SandboxInspectProvider({
181179
sandboxManagerRef.current.stopWatching()
182180
}
183181

184-
const { data } = await supabase.auth.getSession()
185-
186-
if (!data || !data.session) {
187-
router.replace(AUTH_URLS.SIGN_IN)
188-
return
189-
}
190-
191182
const sandbox = await Sandbox.connect(sandboxInfo.sandboxID, {
192183
domain: process.env.NEXT_PUBLIC_E2B_DOMAIN,
193184
// Keep inspect connections from extending sandbox TTL via SDK default connect timeout.
194185
timeoutMs: 1_000,
195186
headers: {
196-
...SUPABASE_AUTH_HEADERS(data.session.access_token, teamId),
187+
...sandboxManagementAuth.headers,
197188
},
198189
})
199190

@@ -209,7 +200,7 @@ export default function SandboxInspectProvider({
209200
team_id: teamId,
210201
root_path: rootPath,
211202
})
212-
}, [sandboxInfo, teamId, rootPath, trackInteraction, router])
203+
}, [sandboxInfo, teamId, rootPath, trackInteraction, sandboxManagementAuth])
213204

214205
// handle sandbox connection / disconnection
215206
useEffect(() => {

0 commit comments

Comments
 (0)