@@ -5,14 +5,15 @@ const AccessToken = Twilio.jwt.AccessToken;
55const VideoGrant = AccessToken . VideoGrant ;
66const MAX_ALLOWED_SESSION_DURATION = 14400 ;
77
8- module . exports . handler = ( context , event , callback ) => {
8+ module . exports . handler = async ( context , event , callback ) => {
99 const {
1010 ACCOUNT_SID ,
1111 TWILIO_API_KEY_SID ,
1212 TWILIO_API_KEY_SECRET ,
1313 API_PASSCODE ,
1414 API_PASSCODE_EXPIRY ,
1515 DOMAIN_NAME ,
16+ ROOM_TYPE ,
1617 } = context ;
1718
1819 const { user_identity, room_name, passcode } = event ;
@@ -30,20 +31,18 @@ module.exports.handler = (context, event, callback) => {
3031 'The passcode used to validate application users has expired. Re-deploy the application to refresh the passcode.' ,
3132 } ,
3233 } ) ;
33- callback ( null , response ) ;
34- return ;
34+ return callback ( null , response ) ;
3535 }
3636
37- if ( API_PASSCODE + appID + serverlessID !== passcode ) {
37+ if ( API_PASSCODE + appID + serverlessID !== passcode . replace ( / \s + / g , '' ) ) {
3838 response . setStatusCode ( 401 ) ;
3939 response . setBody ( {
4040 error : {
4141 message : 'passcode incorrect' ,
4242 explanation : 'The passcode used to validate application users is incorrect.' ,
4343 } ,
4444 } ) ;
45- callback ( null , response ) ;
46- return ;
45+ return callback ( null , response ) ;
4746 }
4847
4948 if ( ! user_identity ) {
@@ -54,8 +53,24 @@ module.exports.handler = (context, event, callback) => {
5453 explanation : 'The user_identity parameter is missing.' ,
5554 } ,
5655 } ) ;
57- callback ( null , response ) ;
58- return ;
56+ return callback ( null , response ) ;
57+ }
58+
59+ const client = context . getTwilioClient ( ) ;
60+
61+ try {
62+ await client . video . rooms . create ( { uniqueName : room_name , type : ROOM_TYPE } ) ;
63+ } catch ( e ) {
64+ if ( e . code !== 53113 ) {
65+ response . setStatusCode ( 401 ) ;
66+ response . setBody ( {
67+ error : {
68+ message : 'error creating room' ,
69+ explanation : 'Something went wrong when creating a room.' ,
70+ } ,
71+ } ) ;
72+ return callback ( null , response ) ;
73+ }
5974 }
6075
6176 const token = new AccessToken ( ACCOUNT_SID , TWILIO_API_KEY_SID , TWILIO_API_KEY_SECRET , {
@@ -65,6 +80,6 @@ module.exports.handler = (context, event, callback) => {
6580 const videoGrant = new VideoGrant ( { room : room_name } ) ;
6681 token . addGrant ( videoGrant ) ;
6782 response . setStatusCode ( 200 ) ;
68- response . setBody ( { token : token . toJwt ( ) } ) ;
69- callback ( null , response ) ;
83+ response . setBody ( { token : token . toJwt ( ) , room_type : ROOM_TYPE } ) ;
84+ return callback ( null , response ) ;
7085} ;
0 commit comments