Skip to content

Instantly share code, notes, and snippets.

@eyalcohen4
Created November 17, 2020 09:45
Show Gist options
  • Save eyalcohen4/0869fd752658c16e001e6558a2ab40e0 to your computer and use it in GitHub Desktop.
Save eyalcohen4/0869fd752658c16e001e6558a2ab40e0 to your computer and use it in GitHub Desktop.
import { RtcTokenBuilder, RtcRole } from 'agora-access-token';
const APP_ID = '';
const APP_CERTIFICATE = '';
export const generateRoomToken = async ({ roomKey, userUid, role, }) => {
if (!roomKey) {
throw new Error('no room name');
}
if (typeof userUid === 'string') {
userUid = parseInt(userUid);
}
let agoraRole;
if (role === 'publisher') {
agoraRole = RtcRole.PUBLISHER;
}
else if (role === 'subscriber') {
agoraRole = RtcRole.SUBSCRIBER;
}
const HOUR = 3600;
const currentTime = Math.floor(Date.now() / 1000);
const privilegeExpireTime = currentTime + HOUR;
return RtcTokenBuilder.buildTokenWithUid(APP_ID, APP_CERTIFICATE, roomKey, userUid, agoraRole, privilegeExpireTime);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment