twilio-voice.js icon indicating copy to clipboard operation
twilio-voice.js copied to clipboard

[BUG] tokenWillExpire listener being triggered just once

Open bgalartza opened this issue 2 years ago • 11 comments

  • [x] I have verified that the issue occurs with the latest twilio.js release and is not marked as a known issue in the CHANGELOG.md.
  • [x] I reviewed the Common Issues and open GitHub issues and verified that this report represents a potentially new issue.
  • [x] I verified that the Quickstart application works in my environment.
  • [x] I am not sharing any Personally Identifiable Information (PII) or sensitive account information (API keys, credentials, etc.) when reporting this issue.

Code to reproduce the issue:

In order to reproduce the issue at least two tokens are needed. To simplify the steps, both tokens were created just before execution the code.

  • token 1 with ttl 120 second
  • token 2 with ttl 240 second
token1 = ""
token2 = ""

dev = new Twilio.Device(token1, {logLevel: 1, debug: true, tokenRefreshMs: 30000 })

dev.on('tokenWillExpire', () => {
    console.log("Token update")
    dev.updateToken(token2);
});

dev.connect()

Expected behavior:

The tokenWillExpire listener should be triggered twice:

  • when token1 is about to expire
  • when token2 is about to expire

So:

  • The "Token update" string should be printed twice in the console.
  • I'm aware that the second execution of the listener would fail because by the time it's executed the token2 would be expired. In reality that should call the BE to obtain a third token, but it was done like that to simplify the code. It's not important for this demo because the code is never executed for the second time.

Actual behavior: The listener is called just once, and the "Token update" string is printed once.

The first time the listener is called as expected:

Token update [debugger eval code:2:13]
Setting token and publishing listen

But there are no more logs after this until token2 is expired and Twilio complains about the expired AccessToken

AccessTokenExpired: AccessTokenExpired (20104): The Access Token provided to the Twilio API has expired, the expiration time specified in the token was invalid, or the expiration time specified was too far in the future

By giving a quick look to the related code I understand that the timer is only initialized when the connected event happens. So the timer is stopped after the first listener trigger, and it's not re-initialized again until a new connection happens. I might be missing something, but maybe the timeout should be initiated after the token update too?

Also, I would expect that the timer is initialized just after you init the Device object, and not when a new call is started. Because it might happen that the access-token is already expired when trying to create an outgoing call or registering for incoming ones.

Software versions:

  • [x] Browser(s): 91.9.0esr (64-bit)
  • [x] Operating System: Debian Bullseye 11
  • [x] twilio.js: 2.1.1
  • [x] Third-party libraries (e.g., Angular, React, etc.): React

bgalartza avatar Jun 03 '22 14:06 bgalartza