diff --git a/v2/functions/src/agora.ts b/v2/functions/src/agora.ts index e6568ab..0c50884 100644 --- a/v2/functions/src/agora.ts +++ b/v2/functions/src/agora.ts @@ -1,5 +1,6 @@ import * as functions from "firebase-functions"; import * as admin from "firebase-admin"; +admin.initializeApp(); // exports.agoraToken = functions.https.onCall((data, context) => { // console.log(data); @@ -11,3 +12,45 @@ import * as admin from "firebase-admin"; // return {token}; // }); + +export const agoraToken = functions.https.onCall((data, context) => { + console.log(data); + + console.log("user's id: " + context.auth?.uid); + const channelName: string = data.channelName; + + let token = getAgoraToken(channelName); + + return { token }; +}); + +import { RtcTokenBuilder, RtcRole } from "agora-access-token"; + +function getAgoraToken(channelName: string) { + // Rtc Examples + const appID = "c8dfd65deb5c4741bd564085627139d0"; + const appCertificate = "aa57b809934b4efcb407cf30089d9f25"; + const uid = 0; // no need to use this as we have authenticated this user + const role = RtcRole.PUBLISHER; + + const expirationTimeInSeconds = 3600; + + const currentTimestamp = Math.floor(Date.now() / 1000); + + const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds; + + // IMPORTANT! Build token with either the uid or with the user account. Comment out the option you do not want to use below. + + // Build token with uid + const tokenA = RtcTokenBuilder.buildTokenWithUid( + appID, + appCertificate, + channelName, + uid, + role, + privilegeExpiredTs + ); + console.log("Token With Integer Number Uid: " + tokenA); + + return tokenA; +} diff --git a/v2/functions/src/index.ts b/v2/functions/src/index.ts index 299fb37..7212124 100644 --- a/v2/functions/src/index.ts +++ b/v2/functions/src/index.ts @@ -7,7 +7,9 @@ admin.initializeApp(); // // https://firebase.google.com/docs/functions/typescript // export const helloWorld = functions.https.onRequest((request, response) => { - functions.logger.info("Hello no!", {structuredData: true}); + functions.logger.info("Hello no!", { structuredData: true }); response.send("Hello from woahhhhhh!"); }); +import { agoraToken } from "./agora"; +exports.agoraToken = agoraToken;