@@ -18,6 +18,12 @@ const LIVEKIT_URL = process.env.LIVEKIT_URL;
1818export const revalidate = 0 ;
1919
2020export async function POST ( req : Request ) {
21+ if ( process . env . NODE_ENV !== 'development' ) {
22+ throw new Error (
23+ 'THIS API ROUTE IS INSECURE. DO NOT USE THIS ROUTE IN PRODUCTION WITHOUT AN AUTHENTICATION LAYER.'
24+ ) ;
25+ }
26+
2127 try {
2228 if ( LIVEKIT_URL === undefined ) {
2329 throw new Error ( 'LIVEKIT_URL is not defined' ) ;
@@ -29,9 +35,10 @@ export async function POST(req: Request) {
2935 throw new Error ( 'LIVEKIT_API_SECRET is not defined' ) ;
3036 }
3137
32- // Parse agent configuration from request body
38+ // Parse room config from request body.
3339 const body = await req . json ( ) ;
34- const agentName : string = body ?. room_config ?. agents ?. [ 0 ] ?. agent_name ;
40+ // Recreate the RoomConfiguration object from JSON object.
41+ const roomConfig = RoomConfiguration . fromJson ( body ?. room_config , { ignoreUnknownFields : true } ) ;
3542
3643 // Generate participant token
3744 const participantName = 'user' ;
@@ -41,15 +48,15 @@ export async function POST(req: Request) {
4148 const participantToken = await createParticipantToken (
4249 { identity : participantIdentity , name : participantName } ,
4350 roomName ,
44- agentName
51+ roomConfig
4552 ) ;
4653
4754 // Return connection details
4855 const data : ConnectionDetails = {
4956 serverUrl : LIVEKIT_URL ,
5057 roomName,
51- participantToken : participantToken ,
5258 participantName,
59+ participantToken,
5360 } ;
5461 const headers = new Headers ( {
5562 'Cache-Control' : 'no-store' ,
@@ -66,7 +73,7 @@ export async function POST(req: Request) {
6673function createParticipantToken (
6774 userInfo : AccessTokenOptions ,
6875 roomName : string ,
69- agentName ?: string
76+ roomConfig : RoomConfiguration
7077) : Promise < string > {
7178 const at = new AccessToken ( API_KEY , API_SECRET , {
7279 ...userInfo ,
@@ -81,10 +88,8 @@ function createParticipantToken(
8188 } ;
8289 at . addGrant ( grant ) ;
8390
84- if ( agentName ) {
85- at . roomConfig = new RoomConfiguration ( {
86- agents : [ { agentName } ] ,
87- } ) ;
91+ if ( roomConfig ) {
92+ at . roomConfig = roomConfig ;
8893 }
8994
9095 return at . toJwt ( ) ;
0 commit comments