How to gracefully fail an automatic token refresh

Jeroen H 40 Reputation points
2025-01-09T09:43:43.33+00:00

Hi all,

I'm using the CallClient from @azure/communication-calling to pick up on and add functionality to incoming Teams calls for users. When creating the teams CallClient, a 'tokenRefresher' can be assigned which periodically calls the method you assign to it. The method has token refresh logic to keep the CallClient working.

With my token refresh method, it is possible that a new token cannot be generated and no token can be returned. This will cause an ugly error in the tokenRefresher itself, and I fear it may crash my application at some point. Because the tokenRefresher is called outside of my managed code, I also can't add any error handling for it.

Is there a way to gracefully fail the tokenRefresher? From what I could tell the documentation recommends to return 'undefined' when no token can be refreshed. However, this still results in a 'Uncaught (in promise) InvalidTokenError: Invalid token specified: must be a string' error which I can't handle.

I understand that there is an abortSingal parameter that I can use, but this only helps with signaling new behavior outside the tokenRefresher, while I want the tokenRefresher itself to abort without error.

Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
959 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
3,471 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sayali-MSFT 3,321 Reputation points Microsoft Vendor
    2025-01-10T08:12:15.7633333+00:00

    Hello @Jeroen H, To gracefully handle the failure of the tokenRefresher in the CallClient from @azure/communication-calling, you can implement a safeguard within your token refresh method to ensure that it does not return an invalid token or undefined. Instead, you can handle the error internally and return a valid token or a fallback mechanism.

    Here is an example of how you can implement such a safeguard in your token refresh method:

    async function tokenRefresher() {
    
      try {
    
        // Replace this with your actual token refresh logic
    
        const newToken = await refreshToken();
    
        if (newToken) {
    
          return newToken;
    
        } else {
    
          // Handle the case where no new token is generated
    
          console.warn("Failed to refresh token. Returning a placeholder token.");
    
          return "placeholder-token"; // Use a valid placeholder token or handle accordingly
    
        }
    
      } catch (error) {
    
        console.error("Error refreshing token:", error);
    
        // Return a valid placeholder token or handle the error gracefully
    
        return "placeholder-token";
    
      }
    
    }
    
    // Initialize CallClient with the tokenRefresher
    
    const callClient = new CallClient({
    
      tokenRefresher: tokenRefresher,
    
      // other parameters
    
    });
    
    

    Reference Document: -https://zcusa.951200.xyz/en-us/microsoftteams/platform/task-modules-and-cards/cards/Universal-actions-for-adaptive-cards/sso-adaptive-cards-universal-action#add-code-to-handle-an-access-token

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.