deploying functions and getting token works
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
import { getFunctions, httpsCallable } from 'firebase/functions';
|
||||||
|
|
||||||
|
import TwilioAccessToken from '@nirvana/core/src/functions/response/TwilioAccessToken.response';
|
||||||
|
|
||||||
|
const functions = getFunctions();
|
||||||
|
|
||||||
|
export const getTwilioAccessToken = httpsCallable(functions, 'getStreamToken');
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import React, { useCallback, useContext, useEffect, useMemo, useRef } from 'react';
|
import React, { useCallback, useContext, useEffect, useMemo, useRef } from 'react';
|
||||||
import { useMediaDevices, useMount } from 'react-use';
|
import { useMediaDevices, useMount } from 'react-use';
|
||||||
|
|
||||||
|
import { getTwilioAccessToken } from '../firebase/functions';
|
||||||
import { useImmer } from 'use-immer';
|
import { useImmer } from 'use-immer';
|
||||||
import { useSnackbar } from 'notistack';
|
import { useSnackbar } from 'notistack';
|
||||||
|
|
||||||
@@ -63,11 +64,33 @@ export function CommunicationsProvider({ children }: { children: React.ReactNode
|
|||||||
|
|
||||||
const devices = useUserDevices();
|
const devices = useUserDevices();
|
||||||
|
|
||||||
|
const getAuthToken = useCallback(async () => {
|
||||||
|
try {
|
||||||
|
const result = await getTwilioAccessToken();
|
||||||
|
console.log(result);
|
||||||
|
enqueueSnackbar('got token data');
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
enqueueSnackbar('something went wrong trying to set up calls', { variant: 'error' });
|
||||||
|
}
|
||||||
|
}, [enqueueSnackbar]);
|
||||||
|
|
||||||
|
useMount(getAuthToken);
|
||||||
|
|
||||||
// const getUserAudioDevices = useMemo(() => {
|
// const getUserAudioDevices = useMemo(() => {
|
||||||
// console.log(devices);
|
// console.log(devices);
|
||||||
// }, [devices]);
|
// }, [devices]);
|
||||||
|
|
||||||
// useMount(getUserAudioDevices);
|
// useMount(getUserAudioDevices);
|
||||||
|
useMount(() => {
|
||||||
|
fetch('https://ui-avatars.com/api/?name=John+Doe')
|
||||||
|
.then((result) => {
|
||||||
|
console.log(result);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CommunicationsContext.Provider value={{ userDeviceSelections }}>
|
<CommunicationsContext.Provider value={{ userDeviceSelections }}>
|
||||||
|
|||||||
-7931
File diff suppressed because it is too large
Load Diff
@@ -20,7 +20,6 @@
|
|||||||
"twilio": "^3.77.2"
|
"twilio": "^3.77.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nirvana/core": "*",
|
|
||||||
"@typescript-eslint/eslint-plugin": "^3.9.1",
|
"@typescript-eslint/eslint-plugin": "^3.9.1",
|
||||||
"@typescript-eslint/parser": "^3.8.0",
|
"@typescript-eslint/parser": "^3.8.0",
|
||||||
"eslint": "^7.6.0",
|
"eslint": "^7.6.0",
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export default class TwilioAccessToken {
|
||||||
|
constructor(public token: string) {}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import * as functions from 'firebase-functions';
|
import * as functions from 'firebase-functions';
|
||||||
import * as twilio from 'twilio';
|
import * as twilio from 'twilio';
|
||||||
|
|
||||||
import TwilioAccessToken from '../../../core/src/functions/response/TwilioAccessToken.response';
|
import TwilioAccessToken from './core/functions/response/TwilioAccessToken.response';
|
||||||
|
|
||||||
const AccessToken = twilio.jwt.AccessToken;
|
const AccessToken = twilio.jwt.AccessToken;
|
||||||
// // Start writing Firebase Functions
|
// // Start writing Firebase Functions
|
||||||
@@ -18,31 +18,36 @@ export const helloWorld = functions.https.onRequest((request, response) => {
|
|||||||
export const getStreamToken = functions.https.onCall((data, context) => {
|
export const getStreamToken = functions.https.onCall((data, context) => {
|
||||||
// Used when generating any kind of tokens
|
// Used when generating any kind of tokens
|
||||||
// To set up environmental variables, see http://twil.io/secure
|
// To set up environmental variables, see http://twil.io/secure
|
||||||
const twilioAccountSid = process.env.TWILIO_ACCOUNT_SID;
|
try {
|
||||||
const twilioApiKey = process.env.TWILIO_API_KEY;
|
const twilioAccountSid = process.env.TWILIO_ACCOUNT_SID;
|
||||||
const twilioApiSecret = process.env.TWILIO_API_SECRET;
|
const twilioApiKey = process.env.TWILIO_API_KEY;
|
||||||
|
const twilioApiSecret = process.env.TWILIO_API_SECRET;
|
||||||
|
|
||||||
if (!twilioAccountSid || !twilioApiKey || !twilioApiSecret) {
|
if (!twilioAccountSid || !twilioApiKey || !twilioApiSecret) {
|
||||||
functions.logger.error('no twilio access tokens!!!');
|
functions.logger.error('no twilio access tokens!!!');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const identity = 'user';
|
||||||
|
|
||||||
|
// Create Video Grant
|
||||||
|
const videoGrant = new VideoGrant();
|
||||||
|
|
||||||
|
// Create an access token which we will sign and return to the client,
|
||||||
|
// containing the grant we just created
|
||||||
|
const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret, {
|
||||||
|
identity: identity,
|
||||||
|
});
|
||||||
|
|
||||||
|
token.addGrant(videoGrant);
|
||||||
|
|
||||||
|
functions.logger.info(`${context.auth?.uid} user granted a stream access token`);
|
||||||
|
|
||||||
|
// Serialize the token to a JWT string
|
||||||
|
return new TwilioAccessToken(token.toJwt());
|
||||||
|
} catch (error) {
|
||||||
|
functions.logger.error(error);
|
||||||
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const identity = 'user';
|
|
||||||
|
|
||||||
// Create Video Grant
|
|
||||||
const videoGrant = new VideoGrant();
|
|
||||||
|
|
||||||
// Create an access token which we will sign and return to the client,
|
|
||||||
// containing the grant we just created
|
|
||||||
const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret, {
|
|
||||||
identity: identity,
|
|
||||||
});
|
|
||||||
|
|
||||||
token.addGrant(videoGrant);
|
|
||||||
|
|
||||||
functions.logger.info(`${context.auth?.uid} user granted a stream access token`);
|
|
||||||
|
|
||||||
// Serialize the token to a JWT string
|
|
||||||
return new TwilioAccessToken(token.toJwt());
|
|
||||||
});
|
});
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user