Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Use backend for team user management

Jakub Novaka94416c8c006
diff --git a/apps/web/src/app/(dashboard)/dashboard/page.tsx b/apps/web/src/app/(dashboard)/dashboard/page.tsx
index 0c3ea82f2..6adf744e5 100644
--- a/apps/web/src/app/(dashboard)/dashboard/page.tsx
+++ b/apps/web/src/app/(dashboard)/dashboard/page.tsx
@@ -6,8 +6,7 @@ import { BarChart, CreditCard, Key, LucideIcon, Settings, Users } from 'lucide-r
import { BillingContent } from '@/components/Dashboard/Billing'
import { TeamContent } from '@/components/Dashboard/Team'
-import { Team, useUser } from '@/utils/useUser'
-import { User } from '@supabase/supabase-js'
+import { E2BUser, Team, useUser } from '@/utils/useUser'
import { KeysContent } from '@/components/Dashboard/Keys'
import { UsageContent } from '@/components/Dashboard/Usage'
import { AccountSelector } from '@/components/Dashboard/AccountSelector'
@@ -32,15 +31,6 @@ export default function Page() {
const { user, isLoading, error } = useUser()
const router = useRouter()
- const [accessToken, setAccessToken] = useState<string | null>(null)
-
- useEffect(() => {
- if (user) {
- setAccessToken(user.accessToken)
- }
- }, [accessToken, user])
-
-
useEffect(() => {
if (isLoading) { return }
if (!user) {
@@ -71,22 +61,12 @@ const Dashboard = ({ user }) => {
const teamParam = searchParams!.get('team')
const [teams, setTeams] = useState<Team[]>([])
const [currentTeam, setCurrentTeam] = useState<Team | null>(null)
- const [currentApiKey, setCurrentApiKey] = useState<string | null>(null)
const initialTab = tab && menuLabels.includes(tab as MenuLabel) ? (tab as MenuLabel) : 'personal'
const [selectedItem, setSelectedItem] = useState<MenuLabel>(initialTab)
const router = useRouter()
-
- useEffect(() => {
- if (user && currentTeam) {
- const apiKey = currentTeam.apiKeys[0]
- setCurrentApiKey(apiKey)
- }
- }, [currentApiKey, currentTeam, user])
-
-
useEffect(() => {
if (user) {
if (teamParam) {
@@ -133,7 +113,7 @@ const Dashboard = ({ user }) => {
<div className="flex-1 md:pl-10">
<h2 className='text-2xl mb-2 font-bold'>{selectedItem[0].toUpperCase() + selectedItem.slice(1)}</h2>
<div className='border border-white/5 w-full h-[1px] mb-10' />
- <MainContent selectedItem={selectedItem} user={user} team={currentTeam} currentApiKey={currentApiKey} accessToken={user.accessToken} teams={teams} setTeams={setTeams} setCurrentTeam={setCurrentTeam} />
+ <MainContent selectedItem={selectedItem} user={user} team={currentTeam} teams={teams} setTeams={setTeams} setCurrentTeam={setCurrentTeam} />
</div>
</>
)
@@ -182,27 +162,25 @@ const MenuItem = ({ icon: Icon, label, selected, onClick }: { icon: LucideIcon;
)
-function MainContent({ selectedItem, user, team, accessToken, currentApiKey, teams, setTeams, setCurrentTeam }: {
+function MainContent({ selectedItem, user, team, teams, setTeams, setCurrentTeam }: {
selectedItem: MenuLabel,
- user: User,
+ user: E2BUser,
team: Team,
- accessToken: string,
- currentApiKey: string | null,
teams: Team[],
setTeams: (teams: Team[]) => void,
setCurrentTeam: (team: Team) => void
}) {
switch (selectedItem) {
case 'personal':
- return <PersonalContent user={user} accessToken={accessToken} />
+ return <PersonalContent user={user} />
case 'keys':
return <KeysContent currentTeam={team} />
case 'usage':
- return <UsageContent currentApiKey={currentApiKey} team={team} />
+ return <UsageContent team={team} />
case 'billing':
- return <BillingContent currentApiKey={currentApiKey} team={team} />
+ return <BillingContent team={team} />
case 'team':
- return <TeamContent team={team} user={user} teams={teams} currentApiKey={currentApiKey} setTeams={setTeams} setCurrentTeam={setCurrentTeam} />
+ return <TeamContent team={team} user={user} teams={teams} setTeams={setTeams} setCurrentTeam={setCurrentTeam} />
default:
return <ErrorContent />
}
diff --git a/apps/web/src/components/Dashboard/Billing.tsx b/apps/web/src/components/Dashboard/Billing.tsx
index 550277039..4617607da 100644
--- a/apps/web/src/components/Dashboard/Billing.tsx
+++ b/apps/web/src/components/Dashboard/Billing.tsx
@@ -18,17 +18,16 @@ interface Invoice {
date_created: string
}
-export const BillingContent = ({ currentApiKey, team }: { currentApiKey: string | null, team: Team }) => {
+export const BillingContent = ({ team }: { team: Team }) => {
const [invoices, setInvoices] = useState<Invoice[]>([])
const [credits, setCredits] = useState<number | null>(null)
useEffect(() => {
const getInvoices = async function getInvoices() {
setInvoices([])
- const res = await fetch(`${process.env.NEXT_PUBLIC_BILLING_API_URL}/teams/${team.id}/invoices`
-, {
+ const res = await fetch(`${process.env.NEXT_PUBLIC_BILLING_API_URL}/teams/${team.id}/invoices`, {
headers: {
- 'X-Team-API-Key': currentApiKey!,
+ 'X-Team-API-Key': team.apiKeys[0],
},
})
if (!res.ok) {
@@ -41,19 +40,17 @@ export const BillingContent = ({ currentApiKey, team }: { currentApiKey: string
setInvoices(invoices)
setCredits(null)
- const creditsRes = await fetch( `${process.env.NEXT_PUBLIC_BILLING_API_URL}/teams/${team.id}/usage`, {
+ const creditsRes = await fetch(`${process.env.NEXT_PUBLIC_BILLING_API_URL}/teams/${team.id}/usage`, {
headers: {
- 'X-Team-API-Key': currentApiKey!,
+ 'X-Team-API-Key': team.apiKeys[0],
},
})
const credits = await creditsRes.json()
setCredits(credits.credits)
}
- if (currentApiKey) {
- getInvoices()
- }
- }
- , [currentApiKey])
+
+ getInvoices()
+ }, [team])
return (
<div className="flex flex-col w-full h-full pb-10">
diff --git a/apps/web/src/components/Dashboard/Personal.tsx b/apps/web/src/components/Dashboard/Personal.tsx
index d8a8e5e50..666103735 100644
--- a/apps/web/src/components/Dashboard/Personal.tsx
+++ b/apps/web/src/components/Dashboard/Personal.tsx
@@ -2,14 +2,14 @@
import { useToast } from '../ui/use-toast'
import { Button } from '../Button'
-import { User } from '@supabase/supabase-js'
import Link from 'next/link'
import { useState } from 'react'
import { Copy } from 'lucide-react'
+import { E2BUser } from '@/utils/useUser'
const updateUserUrl = `${process.env.NEXT_PUBLIC_BILLING_API_URL}/users`
-export const PersonalContent = ({ user, accessToken }: { user: User, accessToken: string }) => {
+export const PersonalContent = ({ user }: { user: E2BUser }) => {
const { toast } = useToast()
const [hovered, setHovered] = useState<boolean>(false)
const [email, setEmail] = useState(user.email)
@@ -29,15 +29,11 @@ export const PersonalContent = ({ user, accessToken }: { user: User, accessToken
}
const updateUserEmail = async () => {
- if (!accessToken) {
- return
- }
-
const res = await fetch(updateUserUrl, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
- 'X-User-Access-Token': `Bearer ${accessToken}`,
+ 'X-User-Access-Token': `Bearer ${user.accessToken}`,
},
body: JSON.stringify({
email,
@@ -112,9 +108,9 @@ export const PersonalContent = ({ user, accessToken }: { user: User, accessToken
className="font-mono cursor-pointer text-xs md:text-sm"
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
- onClick={() => copyToClipboard(accessToken)}
+ onClick={() => copyToClipboard(user.accessToken)}
>
- {hovered ? accessToken : maskAccessToken(accessToken!)}
+ {hovered ? user.accessToken : maskAccessToken(user.accessToken!)}
</div>
</div>
diff --git a/apps/web/src/components/Dashboard/Team.tsx b/apps/web/src/components/Dashboard/Team.tsx
index c60e6688c..4c4a8d760 100644
--- a/apps/web/src/components/Dashboard/Team.tsx
+++ b/apps/web/src/components/Dashboard/Team.tsx
@@ -2,8 +2,7 @@
import { useEffect, useState } from 'react'
import { Button } from '../Button'
-import { Team } from '@/utils/useUser'
-import { User, createPagesBrowserClient } from '@supabase/auth-helpers-nextjs'
+import { E2BUser, Team } from '@/utils/useUser'
import { toast } from '../ui/use-toast'
import { Copy } from 'lucide-react'
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '../ui/table'
@@ -16,9 +15,7 @@ interface TeamMember {
}
-export const TeamContent = ({ team, user, teams, currentApiKey, setTeams, setCurrentTeam }: { team: Team, user: User, teams: Team[], currentApiKey: string | null, setTeams: (teams: Team[]) => void, setCurrentTeam: (team: Team) => void }) => {
- const supabase = createPagesBrowserClient()
-
+export const TeamContent = ({ team, user, teams, setTeams, setCurrentTeam }: { team: Team, user: E2BUser, teams: Team[], setTeams: (teams: Team[]) => void, setCurrentTeam: (team: Team) => void }) => {
const [isDialogOpen, setIsDialogOpen] = useState(false)
const [currentMemberId, setCurrentMemberId] = useState<string | null>(null)
const [isLoading, setIsLoading] = useState<boolean>(true)
@@ -31,7 +28,7 @@ export const TeamContent = ({ team, user, teams, currentApiKey, setTeams, setCur
const getTeamMembers = async () => {
const res = await fetch(`${process.env.NEXT_PUBLIC_BILLING_API_URL}/teams/${team.id}/users`, {
headers: {
- 'X-Team-API-Key': currentApiKey!,
+ 'X-Team-API-Key': team.apiKeys[0],
},
})
@@ -49,10 +46,8 @@ export const TeamContent = ({ team, user, teams, currentApiKey, setTeams, setCur
setIsLoading(false)
}
- if (currentApiKey) {
- getTeamMembers()
- }
- }, [currentApiKey, user, userAdded])
+ getTeamMembers()
+ }, [user, userAdded, team])
useEffect(() => {
setTeamName(team.name)
@@ -65,20 +60,22 @@ export const TeamContent = ({ team, user, teams, currentApiKey, setTeams, setCur
}
const deleteUserFromTeam = async () => {
+ const res = await fetch(`${process.env.NEXT_PUBLIC_BILLING_API_URL}/teams/${team.id}/users`, {
+ method: 'DELETE',
+ headers: {
+ 'X-User-Access-Token': user.accessToken,
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({user_id: currentMemberId}),
+ })
- const { error } = await supabase
- .from('users_teams')
- .delete()
- .eq('user_id', currentMemberId)
- .eq('team_id', team.id)
-
- if (error) {
- // TODO: Add sentry event here
+ if (!res.ok) {
toast({
title: 'An error occurred',
- description: 'We were unable to delete the member from the team',
+ description: 'We were unable to delete the user from the team',
})
- console.log(error)
+ console.log(res.statusText)
+ // TODO: Add sentry event here
return
}
setMembers(members.filter(member => member.id !== currentMemberId))
@@ -87,13 +84,13 @@ export const TeamContent = ({ team, user, teams, currentApiKey, setTeams, setCur
const changeTeamName = async () => {
const res = await fetch(`${process.env.NEXT_PUBLIC_BILLING_API_URL}/teams/${team.id}`, {
- headers: {
- 'X-Team-API-Key': currentApiKey!,
- 'Content-Type': 'application/json',
- },
- method: 'PATCH',
- body: JSON.stringify({ name: teamName }),
- })
+ headers: {
+ 'X-Team-API-Key': team.apiKeys[0],
+ 'Content-Type': 'application/json',
+ },
+ method: 'PATCH',
+ body: JSON.stringify({name: teamName}),
+ })
if (!res.ok) {
toast({
@@ -114,17 +111,21 @@ export const TeamContent = ({ team, user, teams, currentApiKey, setTeams, setCur
}
const addUserToTeam = async () => {
- const { error } = await supabase
- .from('users_teams')
- .insert({ user_id: userToAdd, team_id: team.id })
+ const res = await fetch(`${process.env.NEXT_PUBLIC_BILLING_API_URL}/teams/${team.id}/users`, {
+ headers: {
+ 'X-User-Access-Token': user.accessToken,
+ 'Content-Type': 'application/json',
+ },
+ method: 'POST',
+ body: JSON.stringify({user_id: userToAdd}),
+ })
- if (error) {
- // TODO: Add sentry event here
+ if (!res.ok) {
toast({
- title: 'There was an error adding the user to the team',
- description: 'Please make sure the user id is correct.',
+ title: 'An error occurred',
+ description: 'We were unable to add the user to the team',
})
- console.log(error)
+ console.log(res.statusText)
return
}
diff --git a/apps/web/src/components/Dashboard/Usage.tsx b/apps/web/src/components/Dashboard/Usage.tsx
index 06e64f401..3efe71b5b 100644
--- a/apps/web/src/components/Dashboard/Usage.tsx
+++ b/apps/web/src/components/Dashboard/Usage.tsx
@@ -22,7 +22,7 @@ type Series = {
data: PlotData[]
}
-export const UsageContent = ({ currentApiKey, team }: { currentApiKey: string | null, team: Team }) => {
+export const UsageContent = ({ team }: { team: Team }) => {
const [vcpuData, setVcpuData] = useState<Series[]>([])
const [vcpuHoursThisMonth, setVcpuHoursThisMonth] = useState<number | undefined>()
const [ramData, setRamData] = useState<Series[]>([])
@@ -31,14 +31,14 @@ export const UsageContent = ({ currentApiKey, team }: { currentApiKey: string |
const [costThisMonth, setCostMonth] = useState<number | undefined>()
useEffect(() => {
- const getUsage = async (apiKey: string) => {
+ const getUsage = async () => {
setVcpuData([])
setRamData([])
setCostUsage([])
const response = await fetch( `${process.env.NEXT_PUBLIC_BILLING_API_URL}/teams/${team.id}/usage`, {
headers: {
- 'X-Team-API-Key': apiKey
+ 'X-Team-API-Key': team.apiKeys[0]
}
})
if (!response.ok) {
@@ -65,10 +65,9 @@ export const UsageContent = ({ currentApiKey, team }: { currentApiKey: string |
setRamData(ramSeries)
setRamHoursThisMonth(ramSeries[0].data[ramSeries[0].data.length - 1].y)
}
- if (currentApiKey) {
- getUsage(currentApiKey)
- }
- }, [currentApiKey])
+
+ getUsage()
+ }, [team])
return (
<div className='flex flex-col w-full h-full pb-10'>
diff --git a/apps/web/src/utils/useUser.tsx b/apps/web/src/utils/useUser.tsx
index 826807a08..8d1decd0a 100644
--- a/apps/web/src/utils/useUser.tsx
+++ b/apps/web/src/utils/useUser.tsx
@@ -24,15 +24,17 @@ interface UserTeam {
team_api_keys: { api_key: string; }[];
}
+export type E2BUser = (User & {
+ teams: Team[];
+ accessToken: string;
+ defaultTeamId: string;
+})
+
type UserContextType = {
isLoading: boolean;
session: Session | null;
user:
- | (User & {
- teams: Team[];
- accessToken: string;
- defaultTeamId: string;
- })
+ | E2BUser
| null;
error: Error | null;
wasUpdated: boolean | null;