Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Simplify API keys

Jakub Novak36ac5284df24
diff --git a/apps/docs/src/app/(dashboard)/dashboard/page.tsx b/apps/docs/src/app/(dashboard)/dashboard/page.tsx
index c8001425b..382334e17 100644
--- a/apps/docs/src/app/(dashboard)/dashboard/page.tsx
+++ b/apps/docs/src/app/(dashboard)/dashboard/page.tsx
@@ -61,8 +61,8 @@ export default function Dashboard() {
useEffect(() => {
if (user && currentTeam) {
- const apiKey = user.apiKeys.find(key => key.team_id === currentTeam?.id)
- setCurrentApiKey(apiKey?.api_key)
+ const apiKey = currentTeam.apiKeys[0]
+ setCurrentApiKey(apiKey)
}
}, [currentApiKey, currentTeam, user])
@@ -176,7 +176,7 @@ const MainContent = ({ selectedItem, user, team, accessToken, currentApiKey, tea
case 'personal':
return <PersonalContent user={user} accessToken={accessToken} />
case 'keys':
- return <KeysContent user={user} currentTeam={team} currentApiKey={currentApiKey} />
+ return <KeysContent currentTeam={team} />
case 'usage':
return <UsageContent currentApiKey={currentApiKey} />
case 'billing':
diff --git a/apps/docs/src/components/Dashboard/AccountSelector.tsx b/apps/docs/src/components/Dashboard/AccountSelector.tsx
index cfe2e4e79..eecd57bc6 100644
--- a/apps/docs/src/components/Dashboard/AccountSelector.tsx
+++ b/apps/docs/src/components/Dashboard/AccountSelector.tsx
@@ -41,6 +41,7 @@ export const AccountSelector = ({ teams, user, currentTeam, setCurrentTeam, setT
})
if (!res.ok) {
// TODO: Add sentry event here
+ console.log(res.status, res.statusText)
toast({
title: 'An error occurred',
description: 'We were unable to create the team',
diff --git a/apps/docs/src/components/Dashboard/Keys.tsx b/apps/docs/src/components/Dashboard/Keys.tsx
index eaa5a3fea..52d1a29c3 100644
--- a/apps/docs/src/components/Dashboard/Keys.tsx
+++ b/apps/docs/src/components/Dashboard/Keys.tsx
@@ -15,28 +15,22 @@ import { Copy } from 'lucide-react'
import { useEffect, useState } from 'react'
import { useToast } from '../ui/use-toast'
import { createPagesBrowserClient } from '@supabase/auth-helpers-nextjs'
-import { User } from '@supabase/supabase-js'
import { Team } from '@/utils/useUser'
const createApiKeyUrl = `${process.env.NEXT_PUBLIC_BILLING_API_URL}/teams/api-keys`
-export const KeysContent = ({ user, currentTeam, currentApiKey }: { user: User, currentTeam: Team, currentApiKey: string | null}) => {
+export const KeysContent = ({ currentTeam }: { currentTeam: Team }) => {
const supabase = createPagesBrowserClient()
const { toast } = useToast()
const [isDialogOpen, setIsDialogOpen] = useState(false)
const [currentKey, setCurrentKey] = useState<string | null>(null)
const [hoveredKey, setHoveredKey] = useState<string | null>(null)
- const [apiKeys, setApiKeys] = useState<any[]>([])
+ const [apiKeys, setApiKeys] = useState<string[]>([])
useEffect(() => {
- //@ts-ignore
- const filteredApiKeys = user?.apiKeys.filter((apiKey: any) => apiKey.team_id === currentTeam.id).map((apiKey: any) => ({
- key: apiKey.api_key,
- createdAt: apiKey.created_at,
- }))
- setApiKeys(filteredApiKeys)
- }, [currentTeam, user])
+ setApiKeys(currentTeam.apiKeys)
+ }, [currentTeam])
const closeDialog = () => setIsDialogOpen(false)
const openDialog = (key: string) => {
@@ -71,22 +65,22 @@ export const KeysContent = ({ user, currentTeam, currentApiKey }: { user: User,
return
}
- setApiKeys(apiKeys.filter(apiKey => apiKey.key !== currentKey))
+ setApiKeys(apiKeys.filter(apiKey => apiKey !== currentKey))
closeDialog()
}
const addApiKey = async() => {
-
- if (!currentApiKey) {
- return
+ if (currentTeam === null) {
+ toast({
+ title: 'An error occurred',
+ description: 'We were unable to create the API key',
+ })
}
- const teamId = currentTeam.id
-
const res = await fetch(createApiKeyUrl, {
method: 'POST',
headers: {
- 'X-Team-API-Key': currentApiKey,
+ 'X-Team-API-Key': currentTeam.apiKeys[0],
},
})
@@ -95,6 +89,7 @@ export const KeysContent = ({ user, currentTeam, currentApiKey }: { user: User,
title: 'An error occurred',
description: 'We were unable to create the API key',
})
+ console.log(res.status, res.statusText)
// TODO: Add sentry event here
return
}
@@ -105,7 +100,7 @@ export const KeysContent = ({ user, currentTeam, currentApiKey }: { user: User,
title: 'API key created',
})
- setApiKeys([...apiKeys, {key: newKey.apiKey, team_id: teamId, createdAt: newKey.created_at}])
+ setApiKeys([...apiKeys, newKey.apiKey])
}
const copyToClipboard = (text: string) => {
@@ -139,15 +134,15 @@ export const KeysContent = ({ user, currentTeam, currentApiKey }: { user: User,
>
<div
className="font-mono text-xs md:text-sm"
- onMouseEnter={() => setHoveredKey(apiKey.key)}
+ onMouseEnter={() => setHoveredKey(apiKey)}
onMouseLeave={() => setHoveredKey(null)}
>
- {hoveredKey === apiKey.key ? apiKey.key : maskApiKey(apiKey.key)}
+ {hoveredKey === apiKey ? apiKey : maskApiKey(apiKey)}
</div>
<div className='flex items-center space-x-2'>
- <Copy className='hover:cursor-pointer' width={18} height={18} onClick={() => copyToClipboard(apiKey.key)} />
- <Button className='text-sm' variant='desctructive' onClick={() => openDialog(apiKey.key)}>
+ <Copy className='hover:cursor-pointer' width={18} height={18} onClick={() => copyToClipboard(apiKey)} />
+ <Button className='text-sm' variant='desctructive' onClick={() => openDialog(apiKey)}>
Delete key
</Button>
</div>
diff --git a/apps/docs/src/components/Dashboard/Personal.tsx b/apps/docs/src/components/Dashboard/Personal.tsx
index cf7e77871..86fecc97b 100644
--- a/apps/docs/src/components/Dashboard/Personal.tsx
+++ b/apps/docs/src/components/Dashboard/Personal.tsx
@@ -50,6 +50,8 @@ export const PersonalContent = ({user, accessToken}: {user: User, accessToken: s
title: 'An error occurred',
description: 'We were unable to update the email',
})
+ console.log(res.status, res.statusText)
+ // TODO: Add sentry event here
return
}
diff --git a/apps/docs/src/components/Dashboard/Sandboxes.tsx b/apps/docs/src/components/Dashboard/Sandboxes.tsx
index b52ffc54e..9e4620c91 100644
--- a/apps/docs/src/components/Dashboard/Sandboxes.tsx
+++ b/apps/docs/src/components/Dashboard/Sandboxes.tsx
@@ -9,6 +9,7 @@ import {
import { useState } from 'react'
import { useEffect } from 'react'
+import { Team} from '@/utils/useUser'
interface Sandbox {
sandboxID: string
@@ -19,12 +20,12 @@ interface Sandbox {
clientID: string
}
-export const SandboxesContent = ({ user }) => {
+export const SandboxesContent = ({ team } : { team: Team }) => {
const [runningSandboxes, setRunningSandboxes] = useState<Sandbox[]>([])
useEffect(() => {
const interval = setInterval(() => {
- const apiKey = user?.apiKeys[0]?.api_key
+ const apiKey = team.apiKeys[0]
if (apiKey) {
fetchSandboxes(apiKey).then((newSandboxes) => {
if (newSandboxes) {
@@ -36,7 +37,7 @@ export const SandboxesContent = ({ user }) => {
// Cleanup interval on component unmount
return () => clearInterval(interval)
- }, [user])
+ }, [team])
return (
<div className="flex flex-col justify-center">
diff --git a/apps/docs/src/components/Dashboard/Team.tsx b/apps/docs/src/components/Dashboard/Team.tsx
index cac3c5bdf..59f96e8f8 100644
--- a/apps/docs/src/components/Dashboard/Team.tsx
+++ b/apps/docs/src/components/Dashboard/Team.tsx
@@ -40,7 +40,7 @@ export const TeamContent = ({ team, user, teams, currentApiKey, setTeams, setCur
description: 'We were unable to fetch the team members',
})
console.log(res.statusText)
-
+ // TODO: Add sentry event here
return
}
const members = (await res.json()).filter((member: TeamMember) => member.id !== user.id)
diff --git a/apps/docs/src/utils/useUser.tsx b/apps/docs/src/utils/useUser.tsx
index 224094722..175e73ca4 100644
--- a/apps/docs/src/utils/useUser.tsx
+++ b/apps/docs/src/utils/useUser.tsx
@@ -12,6 +12,16 @@ export type Team = {
tier: string
is_default: boolean
email: string
+ apiKeys: string[]
+}
+
+interface UserTeam {
+ id: string;
+ name: string;
+ is_default: boolean;
+ tier: string;
+ email: string;
+ team_api_keys: { api_key: string; }[];
}
type UserContextType = {
@@ -20,7 +30,6 @@ type UserContextType = {
user:
| (User & {
teams: Team[];
- apiKeys: any[];
accessToken: string;
defaultTeamId: string;
pricingTier: {
@@ -102,13 +111,16 @@ export const CustomUserContextProvider = (props) => {
// @ts-ignore
const { data: userTeams, teamsError } = await supabase
.from('users_teams')
- .select('teams (id, name, is_default, tier, email)')
+ .select('teams (id, name, is_default, tier, email, team_api_keys (api_key))')
.eq('user_id', session?.user.id) // Due to RLS, we could also safely just fetch all, but let's be explicit for sure
if (teamsError) Sentry.captureException(teamsError)
// TODO: Adjust when user can be part of multiple teams
// @ts-ignore
- const teams = userTeams?.map(userTeam => userTeam.teams as Team)
+ const teams = userTeams?.map(userTeam => userTeam.teams).map((team: UserTeam) => ({
+ ...team,
+ apiKeys: team.team_api_keys.map(apiKey => apiKey.api_key)
+ } as Team))
const defaultTeam = teams?.find(team => team.is_default)
@@ -132,11 +144,11 @@ export const CustomUserContextProvider = (props) => {
promoEndsAt = promoData?.[0]?.promo_ends_at
}
- const { data: apiKeys, error: apiKeysError } = await supabase
- .from('team_api_keys')
- .select('*')
- .in('team_id', (teams?.map((team) => team.id) as any)) // Due to RLS, we could also safely just fetch all, but let's be explicit for sure
- if (apiKeysError) Sentry.captureException(apiKeysError)
+ // const { data: apiKeys, error: apiKeysError } = await supabase
+ // .from('team_api_keys')
+ // .select('*')
+ // .in('team_id', (teams?.map((team) => team.id) as any)) // Due to RLS, we could also safely just fetch all, but let's be explicit for sure
+ // if (apiKeysError) Sentry.captureException(apiKeysError)
const defaultTeamId = defaultTeam?.id // TODO: Adjust when user can be part of multiple teams
@@ -148,13 +160,13 @@ export const CustomUserContextProvider = (props) => {
.single()
if (accessTokenError) Sentry.captureException(accessTokenError)
+ console.log('teams', teams)
setUser({
...session?.user,
teams: teams ?? [],
- apiKeys: apiKeys ?? [],
accessToken: accessToken?.access_token,
defaultTeamId,
- error: teamsError || apiKeysError,
+ error: teamsError,
pricingTier: {
id: pricingTier,
isPromo: isPromoTier,
@@ -208,7 +220,7 @@ export const useUser = (): UserContextType => {
export const useApiKey = (): string => {
// for convenience
const { user } = useUser()
- return user?.apiKeys?.[0]?.api_key
+ return user ? user?.teams[0].apiKeys[0] : ''
}
export const useAccessToken = () => {