Twitch token sometimes not being refreshed on time?

Hello,

I have an extension that needs to send a number, selected on click, to my EBS and with user ID.

Sometimes, for some users, the EBS fails to extract the Twitch ID from token because the latter is expired: TokenExpiredError: jwt expired

The extension was likely running for more than 1 hour at impacted users, but the extension is working for a vast majority of users that are on the stream for more than 1 hour. So I suspect that the token is sometimes not refreshed after expiration.

I have already read this post that describe the problem I could have, adjusted my code accordingly and still have issues.

I have also read this post, but I would like to insure my code is correct before to explore 3rd-party issues.

Here is my process:

  • I’m subscribing to the onAuthorized event to trigger token refresh:
private constructor() {
    this._twitchExtension = window["Twitch"]?.ext;
    this._twitchExtension?.onAuthorized(this.handle_onAuthorized.bind(this));
}
  • And then I store the authentication object into the _authentifier attribute:
public handle_onAuthorized(authentifier: any): void {
    this._authentifier = authentifier;

    // Only notify the first time for app start processings
    this.triggerOnceAuthenticatedListeners();
}
  • So, when I need to send the number user has selected to my EBS, I use the _authentifier object to get the token and include it into the request body:
public getPlayerToken(): string {
    return this._authentifier.token;
}

Is there something wrong with this implementation ?

Why storing in your own object instead of using window.Twitch.ext.viewer.sessionToken

Granted they should be one and the same

Sounds like this could be Issues with Panel Extensions loading on profile pages · Issue #798 · twitchdev/issues · GitHub if the page was open an hour and then the user scrolled down, but thats localised to panels only generall speaking.

Theres a number of other things that could interfer (ad/script or other third party browser extensions or anything that interfers with normal page operation or pure connection loss to Twitch)

Without know what led to an expired token theres little to suggest.

Your code seems fine. Personally I’d use window.Twitch.ext.viewer.sessionToken when constructing my ebs calls however

Why storing in your own object instead of using window.Twitch.ext.viewer.sessionToken

I used this in my previous implementation and already has the issue, so I switched to this second option wich appeared a bit more robust by providing a periodically renewed object (rather than a persistent one).

As the error is still happening, I will switch back to window.Twitch.ext.viewer.sessionToken which is less complex to maintain.

And now I can at least eliminate this cause and check other possibilities you mentioned.

Thank you!