feat: Add support for participant name, metadata, and attributes (#142)

This commit is contained in:
Mahmoud Hemaid
2025-05-29 02:05:22 +03:00
committed by GitHub
parent 575da78aa1
commit 9e2b7fcc61
6 changed files with 204 additions and 11 deletions

View File

@@ -19,20 +19,38 @@ export default async function handleToken(
res: NextApiResponse
) {
try {
if (req.method !== "POST") {
res.setHeader("Allow", "POST");
res.status(405).end("Method Not Allowed");
return;
}
if (!apiKey || !apiSecret) {
res.statusMessage = "Environment variables aren't set up correctly";
res.status(500).end();
return;
}
const {
roomName: roomNameFromBody,
participantName,
participantId: participantIdFromBody,
metadata: metadataFromBody,
attributes: attributesFromBody,
} = req.body;
// Get room name from query params or generate random one
const roomName = req.query.roomName as string ||
const roomName = roomNameFromBody as string ||
`room-${generateRandomAlphanumeric(4)}-${generateRandomAlphanumeric(4)}`;
// Get participant name from query params or generate random one
const identity = req.query.participantName as string ||
const identity = participantIdFromBody as string ||
`identity-${generateRandomAlphanumeric(4)}`;
// Get metadata and attributes from query params
const metadata = metadataFromBody as string | undefined;
const attributesStr = attributesFromBody as string | undefined;
const attributes = attributesStr || {};
const grant: VideoGrant = {
room: roomName,
roomJoin: true,
@@ -41,7 +59,7 @@ export default async function handleToken(
canSubscribe: true,
};
const token = await createToken({ identity }, grant);
const token = await createToken({ identity, metadata, attributes, name: participantName }, grant);
const result: TokenResult = {
identity,
accessToken: token,