Skip to content

Instantly share code, notes, and snippets.

@perkinsjr
Created April 27, 2021 15:37
Show Gist options
  • Save perkinsjr/d48be9919d59d803b638c6af4df50030 to your computer and use it in GitHub Desktop.
Save perkinsjr/d48be9919d59d803b638c6af4df50030 to your computer and use it in GitHub Desktop.
const functions = require("firebase-functions");
exports.getToken =
functions.https.onRequest((req, res) => {
res.set("Access-Control-Allow-Origin", "SET ORIGIN");
if (req.method === "OPTIONS") {
// Send response to OPTIONS requests
res.set("Access-Control-Allow-Methods", "GET");
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set("Access-Control-Max-Age", "3600");
res.status(204).send("");
} else {
const username = req.body.identity;
const room = req.body.room;
const roomType = req.body.type
const AccessToken = require("twilio").jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
// Substitute your Twilio AccountSid and ApiKey details
// Substitute your Twilio AccountSid and ApiKey details
const ACCOUNT_SID = process.env.TWILIO_ACCOUNT_SID;
const API_KEY_SID = process.env.TWILIO_API_KEY_SID;
const API_KEY_SECRET = process.env.TWILIO_API_KEY_SECRET;
// Create an Access Token
const token = new AccessToken(ACCOUNT_SID, API_KEY_SID, API_KEY_SECRET);
token.identity = username;
const videoGrant = new VideoGrant({
room: room,
type: roomType,
});
// Add the grant to the token
token.addGrant(videoGrant);
// Serialize the token as a JWT
const jwt = token.toJwt();
res.status(200).json({
token: jwt,
room_type: "go",
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment