question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sample of token expiration handling

See original GitHub issue

Feature Request

Is your feature request related to a problem? Please describe.

It is not clear how to handle tokens after they expires. All the samples in this repo call the token API once and not handling token expiration.

Currently, I handle the token expiration on my own - when the token is about to expire, I get a refreshed token use createDirectLine({ token }) with the new token. I’m not sure if this the desired approach. Since I believe that the directline should be created once (the current conversation will be interrupted)

Describe the suggestion or request in detail

Please add a sample to this repo of how to handle token expiration.

Describe alternatives you have considered

Additional context

[feature-request]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
stevkancommented, Mar 3, 2021

@hraz-msft, I suspect the issue is how you are introducing the refreshed token into your Web Chat component. In the example I provide below, I am able to refresh the token while maintaining the conversation context without issue.

In short, I get my initial token and pass it into a function, createDirectLine, to create a Direct Line object with it. The output of this is passed into startWebChat which initiates renderWebChat. Lastly, I have a function, conversationTimer, which is called immediately to start the countdown.

When the countdown is completed, a refreshed token is acquired, createDirectLine is called again with the refreshed token passed into it followed by calling startWebChat once more which reconnects the Web Chat component using the new token. Then the countdown/process begins, again.

FYI, I use locally hosted endpoints for calling a “token server” where I exchange my Direct Line secret for a token. I also set the timer to just 15 secs for effect.

<script>
  ( async function () {

    let directLine;

    const refreshToken = async () => {
      const res2 = await fetch( 'http://localhost:3500/directline/refresh', {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify( { token: sessionStorage.getItem( 'token' ) } )
      } );
      const { token: token2 } = await res2.json();
      return token2;
    }

    const conversationTimer = () => {
      setInterval( async () => {
        const sessionToken = await refreshToken();
        sessionStorage.setItem( 'token', sessionToken );
        directLine = createDirectLine( sessionToken )
        startWebChat();
      }, 15000 );
    };

    conversationTimer();

    const createDirectLine = ( token ) => {
      const dl = window.WebChat.createDirectLine( {
        token
      } )
      return dl;
    }

    const res1 = await fetch( 'http://localhost:3500/directline/conversations', { method: 'POST' } );
    const { token: token1 } = await res1.json();
    sessionStorage.setItem( 'token', token1 );
    
    directLine = createDirectLine(token1);

    const startWebChat = () => {
      window.WebChat.renderWebChat(
        {
          directLine: directLine
        },
        document.getElementById( 'webchat' )
      );
      document.querySelector( '#webchat > *' ).focus();
    }

    startWebChat();

  } )().catch( err => console.error( err ) );
</script>
0reactions
hrazmsftcommented, Mar 3, 2021

Thanks @stevkan for this sample! I think I’ve found the issue on our side. I’m closing the issue now, and I will re-open it again and tag you if other issues will come up due to the refresh

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configure Refresh Token Expiration - Auth0
Learn how to configure the refresh token expiration lifetimes. ... Here is an example that sets expiration lifetime for a non-rotating refresh token:....
Read more >
Handling Access Token Expiration - Oracle Help Center
Using an expired JWT will cause operations to fail. As you saw above, we are told how long a token is valid through...
Read more >
Managing JWT token expiration - Medium
Check if the token has expired. If the token is expired we clean up the existing token, application state and redirect the user...
Read more >
Handling Expired Access Tokens | MuleSoft Documentation
Most services providers return (or should return) access tokens with a limited lifespan. In general, access tokens usually expire about 30 to 60...
Read more >
Specify the maximum token expiration time—Portal for ArcGIS
A token with a longer expiration time is less secure. For example, a token intercepted by a malicious user can be used until...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found