add jwt examples

This commit is contained in:
Kritarth 2020-04-28 10:44:47 +05:30
parent 48aeba2fef
commit ac927714b3
2 changed files with 15 additions and 2 deletions

13
examples/jwt.js Normal file
View file

@ -0,0 +1,13 @@
var Plivo = require('../dist/rest/client.js');
// token generation using from epoch and length in seconds
var acctkn = new Plivo.AccessToken('{authId}', '{authToken}', 'endpointUsername', {validFrom: (new Date()).getTime()/1000, lifetime: 300}, 'username-12345');
console.log(acctkn.toJwt());
// token with validity of 24 hours starting now
acctkn = new Plivo.AccessToken('{authId}', '{authToken}', 'endpointUsername', {}, 'uid');
// voice (incoming, outgoing) grants
acctkn.addVoiceGrants(true, true);
console.log(acctkn.toJwt());

View file

@ -25,10 +25,10 @@ export function AccessToken(authId, authToken, username, validityOptions = {}, u
if (validityOptions.validTill != null) {
this.lifetime = validityOptions.validTill - this.validFrom;
} else {
this.lifetime = 84600;
this.lifetime = 86400;
}
} else {
this.lifetime = validityOptions.lifetime || 84600;
this.lifetime = validityOptions.lifetime || 86400;
if (validityOptions.validTill != null) {
this.validFrom = validityOptions.validTill - this.lifetime;
} else {