Skip to content

Commit c539a4a

Browse files
Pass sandbox management auth from server (#358)
1 parent d0aea9a commit c539a4a

16 files changed

Lines changed: 124 additions & 81 deletions

File tree

bun.lock

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
"cmdk": "^1.0.4",
102102
"date-fns": "^4.1.0",
103103
"date-fns-tz": "^3.2.0",
104-
"e2b": "^2.14.0",
104+
"e2b": "^2.27.1",
105105
"echarts": "^6.0.0",
106106
"echarts-for-react": "^3.0.2",
107107
"fast-xml-parser": "^5.3.5",
Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,53 @@
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+
sandboxId: string
15+
teamSlug: string
16+
}>
17+
}
18+
19+
export default async function SandboxInspectPage({
20+
params,
21+
}: SandboxInspectPageProps) {
22+
const [{ teamSlug }, cookieStore, authContext] = await Promise.all([
23+
params,
24+
cookies(),
25+
auth.getAuthContext(),
26+
])
27+
28+
if (!authContext) {
29+
redirect(AUTH_URLS.SIGN_IN)
30+
}
31+
32+
const teamId = await getTeamIdFromSlug(teamSlug, authContext.accessToken)
33+
if (!teamId.ok) {
34+
throw new Error('Failed to resolve team for sandbox filesystem')
35+
}
36+
if (!teamId.data) {
37+
redirect(PROTECTED_URLS.DASHBOARD)
38+
}
39+
940
const rootPath =
1041
cookieStore.get(COOKIE_KEYS.SANDBOX_INSPECT_ROOT_PATH)?.value ||
1142
DEFAULT_ROOT_PATH
1243

13-
return <SandboxInspectView rootPath={rootPath} />
44+
return (
45+
<SandboxInspectView
46+
rootPath={rootPath}
47+
sandboxManagementAuth={createSandboxManagementAuth(
48+
authContext,
49+
teamId.data
50+
)}
51+
/>
52+
)
1453
}

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/modules/auth/models'
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/modules/auth/models.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import z from 'zod'
22
import { httpUrlSchema } from '@/core/shared/schemas/url'
33

4+
export type AuthUser = {
5+
id: string
6+
email: string | null
7+
name: string | null
8+
avatarUrl: string | null
9+
providers: string[]
10+
canChangeEmail: boolean
11+
canChangePassword: boolean
12+
}
13+
414
export const OtpTypeSchema = z.enum([
515
'signup',
616
'recovery',

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/modules/auth/models'
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/modules/auth/models'
2+
3+
export type { AuthUser } from '@/core/modules/auth/models'
104

115
export type AuthContext = {
126
user: AuthUser
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+
}

0 commit comments

Comments
 (0)