Add firebase cloud function sample
Is your feature request related to a problem? Please describe.
Sample code for other firebase users may save them a lot of time.
Describe the solution you'd like
Add this to the docs:
const { RtcTokenBuilder, RtcRole } = require('agora-access-token')
const functions = require("firebase-functions")
export const generateToken = functions.https.onCall((data: any, context: any) => {
const request_data = data;
const config = functions.config().agora;
const appID = config.app_ID;
const appCertificate = config.app_certificate;
const expirationTimeInSeconds = 60 * 60 * config.token_expires_hours;
const channelName = request_data.channel;
const uid = 0;
const role = RtcRole.PUBLISHER;
const currentTimestamp = Math.floor(Date.now() / 1000)
const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds
const token = RtcTokenBuilder.buildTokenWithUid(appID, appCertificate, channelName, uid, role, privilegeExpiredTs);
return {
'token': token,
'channel': channelName,
};
});
I wrote this code anyway and I saw similar code samples in other flutter projects - I feel it may be a useful and easy add to the docs.
Additional context
#359 is sort of a blocker or people will be very disappointed.
I will create a wiki page to explain your issue.
@sils I've been trying with this code for quite some time, however deploying the functions to firebase seems to fail on the following code:
const { RtcTokenBuilder, RtcRole } = require('agora-access-token')
Did you manage to find a solution for that?
Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. If you find this problem please file a new issue with the same description, what happens, logs and the output. All system setups can be slightly different so it's always better to open new issues and reference the related ones. Thanks for your contribution.
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please raise a new issue.