Skip to content

Commit 90e871f

Browse files
Add: sandbox creation route under /sbx/new (#183)
<!-- CURSOR_SUMMARY --> > [!NOTE] > Adds an authenticated GET route at `/sbx/new` that creates an E2B sandbox and redirects to its inspect page, with error handling and sign-in redirects. > > - **Routes**: > - **New GET** `src/app/sbx/new/route.ts` > - Auth guard via Supabase (`createClient`, `auth.getUser`) and `getSessionInsecure`; redirects to `AUTH_URLS.SIGN_IN` with `returnTo` if unauthenticated. > - Fetches default team with `getDefaultTeam` and creates E2B sandbox via `Sandbox.create('base', { domain: env, headers: SUPABASE_AUTH_HEADERS(token, teamId) })`. > - Redirects to `PROTECTED_URLS.SANDBOX_INSPECT(teamSlug, sandboxId)` on success. > - On error: logs (`l.warn` with `serializeError`) and redirects to request origin. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit d322ab9. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 95fbd03 commit 90e871f

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/app/sbx/new/route.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { SUPABASE_AUTH_HEADERS } from '@/configs/api'
2+
import { AUTH_URLS, PROTECTED_URLS } from '@/configs/urls'
3+
import { l } from '@/lib/clients/logger/logger'
4+
import { createClient } from '@/lib/clients/supabase/server'
5+
import { getDefaultTeam } from '@/server/auth/get-default-team'
6+
import { getSessionInsecure } from '@/server/auth/get-session'
7+
import Sandbox from 'e2b'
8+
import { NextRequest, NextResponse } from 'next/server'
9+
import { serializeError } from 'serialize-error'
10+
11+
export const GET = async (req: NextRequest) => {
12+
try {
13+
const supabase = await createClient()
14+
const { data, error } = await supabase.auth.getUser()
15+
16+
if (error || !data.user) {
17+
const params = new URLSearchParams({
18+
returnTo: new URL(req.url).pathname,
19+
})
20+
21+
return NextResponse.redirect(
22+
new URL(`${AUTH_URLS.SIGN_IN}?${params.toString()}`, req.url)
23+
)
24+
}
25+
26+
const session = await getSessionInsecure(supabase)
27+
28+
if (!session) {
29+
const params = new URLSearchParams({
30+
returnTo: new URL(req.url).pathname,
31+
})
32+
33+
return NextResponse.redirect(
34+
new URL(`${AUTH_URLS.SIGN_IN}?${params.toString()}`, req.url)
35+
)
36+
}
37+
38+
const defaultTeam = await getDefaultTeam(data.user.id)
39+
40+
const sbx = await Sandbox.create('base', {
41+
domain: process.env.NEXT_PUBLIC_E2B_DOMAIN,
42+
headers: {
43+
...SUPABASE_AUTH_HEADERS(session.access_token, defaultTeam.id),
44+
},
45+
})
46+
47+
const inspectUrl = PROTECTED_URLS.SANDBOX_INSPECT(
48+
defaultTeam.slug,
49+
sbx.sandboxId
50+
)
51+
52+
return NextResponse.redirect(new URL(inspectUrl, req.url))
53+
} catch (error) {
54+
l.warn(
55+
{
56+
key: 'sbx_new:unexpected_error',
57+
error: serializeError(error),
58+
},
59+
`sbx_new: unexpected error`
60+
)
61+
62+
return NextResponse.redirect(new URL(req.url).origin)
63+
}
64+
}

0 commit comments

Comments
 (0)