Skip to content

Commit

Permalink
💩(frontend) add PoC calling LiveKit server with an access token
Browse files Browse the repository at this point in the history
This code is shit. It was made to play with the API.
@manuhabitela, the floor is yours.
  • Loading branch information
lebaudantoine committed Jun 25, 2024
1 parent 34301af commit b2bbe68
Showing 1 changed file with 70 additions and 10 deletions.
80 changes: 70 additions & 10 deletions src/frontend/visio/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {useEffect, useState} from 'react'
import './App.css'
import {LiveKitRoom, VideoConference} from "@livekit/components-react";

import '@livekit/components-styles';

const API_BASE_URL = 'http://localhost:8071/api/v1.0/'
const LIVEKIT_SERVER_URL = 'http://localhost:7880'

export interface User {
id: string;
Expand Down Expand Up @@ -34,9 +38,41 @@ async function getMe() {
return response.json() as Promise<User>;
}

function slugify(str) {
return str
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.trim()
.replace(/[^a-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
}

async function fetchRoom(roomId) {
const csrfToken = getCSRFToken();
const response = await fetch(
`${API_BASE_URL}rooms/${roomId}/`,
{
credentials: 'include',
headers: {
'Content-Type': 'application/json',
...(csrfToken && {'X-CSRFToken': csrfToken}),
},
}
)

if (!response.ok) {
throw new Error(`Couldn't fetch room data: ${response.statusText}`);
}

return response.json() as Promise<User>;
}


function App() {
const [authenticatedUser, setAuthenticatedUser] = useState<User | null>(null)
const [roomData, setRoomData] = useState(null)
const [isLoading, setIsLoading] = useState<boolean>(true)

useEffect(() => {
Expand All @@ -48,6 +84,17 @@ function App() {
.finally(() => setIsLoading(false))
}, [])

const getRoom = async () => {

const roomName = document.getElementById('roomName')?.value || crypto.randomUUID()
const roomData = await fetchRoom(slugify(roomName))
setRoomData(roomData)

if (!roomData.livekit && !roomData.is_public) {
alert('no access')
}
}

if (isLoading) {
return (
<div>
Expand All @@ -56,17 +103,30 @@ function App() {
)
}

if (authenticatedUser) {
return (
<div style={{display: 'flex', flexDirection: 'column'}}>
<p>You are connected as: {authenticatedUser.email}</p>
<button onClick={() => window.location.replace(new URL('logout/', API_BASE_URL).href)}>Logout</button>
</div>
)
}

return (
<button onClick={() => window.location.replace(new URL('authenticate/', API_BASE_URL).href)}>Login</button>
<div>
{
authenticatedUser ? (
<div style={{display: 'flex', flexDirection: 'column'}}>
<p>You are connected as: {authenticatedUser.email}</p>
<button onClick={() => window.location.replace(new URL('logout/', API_BASE_URL).href)}>Logout</button>
</div>
) : (
<button onClick={() => window.location.replace(new URL('authenticate/', API_BASE_URL).href)}>Login</button>
)
}
<>
<input id="roomName" type="text" placeholder="Room name"/>
<button onClick={() => getRoom()}>Get room</button>
</>
{
roomData?.livekit && (
<LiveKitRoom serverUrl={LIVEKIT_SERVER_URL} token={roomData?.livekit?.token} connect={true}>
<VideoConference/>
</LiveKitRoom>
)
}
</div>
)
}

Expand Down

0 comments on commit b2bbe68

Please sign in to comment.