Skip to content

Commit 898dca7

Browse files
author
Sam Harrison
committed
fix: correct the Node AccessToken examples to be more compatible with TypeScript
Fixes twilio/twilio-node#579 The `identity` property is not accessible via the TS definition but can be set using the constructor (along with `ttl` and `nbf`).
1 parent ad1aaaa commit 898dca7

File tree

6 files changed

+24
-17
lines changed

6 files changed

+24
-17
lines changed

ip-messaging/users/token-generation-server/token-generation-server.3.x.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ app.get('/token', (request, response) => {
3333
const token = new AccessToken(
3434
process.env.TWILIO_ACCOUNT_SID,
3535
process.env.TWILIO_API_KEY,
36-
process.env.TWILIO_API_SECRET
36+
process.env.TWILIO_API_SECRET,
37+
{identity: identity}
3738
);
3839
token.addGrant(chatGrant);
39-
token.identity = identity;
4040

4141
// Serialize the token to a JWT string and include it in a JSON response
4242
response.send({

rest/access-tokens/ip-messaging-example/ip-messaging-example.3.x.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ const chatGrant = new ChatGrant({
1818

1919
// Create an access token which we will sign and return to the client,
2020
// containing the grant we just created
21-
const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret);
21+
const token = new AccessToken(
22+
twilioAccountSid,
23+
twilioApiKey,
24+
twilioApiSecret,
25+
{identity: identity}
26+
);
2227

2328
token.addGrant(chatGrant);
2429

25-
token.identity = identity;
26-
2730
// Serialize the token to a JWT string
2831
console.log(token.toJwt());

rest/access-tokens/video-example/video-example.3.x.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ const videoGrant = new VideoGrant({
1515

1616
// Create an access token which we will sign and return to the client,
1717
// containing the grant we just created
18-
const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret);
18+
const token = new AccessToken(
19+
twilioAccountSid,
20+
twilioApiKey,
21+
twilioApiSecret,
22+
{identity: identity}
23+
);
1924
token.addGrant(videoGrant);
20-
token.identity = identity;
2125

2226
// Serialize the token to a JWT string
2327
console.log(token.toJwt());

rest/access-tokens/voice-example/voice-example.3.x.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ const voiceGrant = new VoiceGrant({
1818

1919
// Create an access token which we will sign and return to the client,
2020
// containing the grant we just created
21-
const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret);
21+
const token = new AccessToken(
22+
twilioAccountSid,
23+
twilioApiKey,
24+
twilioApiSecret,
25+
{identity: identity}
26+
);
2227
token.addGrant(voiceGrant);
23-
token.identity = identity;
2428

2529
// Serialize the token to a JWT string
2630
console.log(token.toJwt());

video/users/token-generation-server-rooms/token-generation-server.3.x.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ app.get('/token', (request, response) => {
2020
const token = new AccessToken(
2121
process.env.TWILIO_ACCOUNT_SID,
2222
process.env.TWILIO_API_KEY,
23-
process.env.TWILIO_API_SECRET
23+
process.env.TWILIO_API_SECRET,
24+
{identity: identity}
2425
);
2526

26-
// Assign the generated identity to the token.
27-
token.identity = identity;
28-
2927
// Grant the access token Twilio Video capabilities.
3028
const grant = new VideoGrant();
3129
grant.configurationProfileSid = process.env.TWILIO_CONFIGURATION_SID;

video/users/token-generation-server/token-generation-server.3.x.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ app.get('/token', (request, response) => {
2020
const token = new AccessToken(
2121
process.env.TWILIO_ACCOUNT_SID,
2222
process.env.TWILIO_API_KEY,
23-
process.env.TWILIO_API_SECRET
23+
process.env.TWILIO_API_SECRET,
24+
{identity: identity}
2425
);
2526

26-
// Assign the generated identity to the token.
27-
token.identity = identity;
28-
2927
// Grant access to Video.
3028
const grant = new VideoGrant();
3129
grant.configurationProfileSid = process.env.TWILIO_CONFIGURATION_SID;

0 commit comments

Comments
 (0)