adding in the agora cloud function

This commit is contained in:
Arjun Patel
2022-01-27 19:59:55 -08:00
parent a4d2ccbc55
commit ed1e68b5e1
2 changed files with 46 additions and 1 deletions
+43
View File
@@ -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;
}
+3 -1
View File
@@ -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;