How to subscribe to the refresh token event

◇◆丶佛笑我妖孽 提交于 2021-02-10 21:53:44

问题


The problem is that I need to be able to subscribe to the token refresh event and I can't figure out how.

I know people advise on subscribing to connectionStatus$ and handling the ConnectionStatus.ExpiredToken case, but the execution never enters that case when refreshing happens, it only enters that case when I try to initialize the bot with an expired token.

The token refresh event is getting triggered every 15 minutes by the library itself but there is no observable that will allow me to subscribe to it to get the newly refreshed token. The workaround I found for now is that I set an interval of 15 min (by using setInterval()) for checking if the token used by the connection has changed.

Any idea?

The code is in pure, vanilla, javascript. Library code I'm using: https://cdn.botframework.com/botframework-webchat/master/webchat.js


回答1:


If you are wanting to capture the token, then I would recommend you save it during the API call you make to retrieve it. As you haven't provided any code, I can only guess at what your setup looks like. The below is a simplified example demonstrating getting a token (and saving it to sessionStorage) if no token exists. If a token does exist it's then refreshed every 25 mins (and saved to sessionStorage) as at 30 mins it is in danger of expiring with no activity.

let { token, conversationId } = sessionStorage;

if ( !token ) {
  let res = await fetch( 'http://localhost:3500/directline/token', { method: 'POST' } );

  const { token: directLineToken, conversationId, error } = await res.json();
  sessionStorage[ 'token' ] = directLineToken;
  sessionStorage[ 'conversationId' ] = conversationId;
  token = directLineToken;
}

if (token) {
  await setInterval(async () => {
    let res = await fetch( 'http://localhost:3500/directline/refresh', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify( { token: token } )
    } );
    const { token: directLineToken, conversationId } = await res.json();
    sessionStorage[ 'token' ] = directLineToken;
    sessionStorage[ 'conversationId' ] = conversationId;
    token = directLineToken;
  }, 150000)
}

It is possible to subscribe to connectionStatus$, however that will only show if a connection was made but an error was encountered regarding that connection. If there is a token issue that restricts Web Chat from even connecting, then the observable will never be reached.

const store = window.WebChat.createStore( {}, ( { dispatch } ) => next => async action => {

  if(action.payload && action.payload.directLine) {
    const subscription = action.payload.directLine.connectionStatus$.subscribe( {
      error: error => console.log( error ),
      next: value => {
        if ( value === 2 ) {
          console.log('Connected')
        } else if ( value === 4 ) {
          console.log('Encountered a connection error')
        }
      }
    } );
  }
}

Hope of help!



来源:https://stackoverflow.com/questions/59247586/how-to-subscribe-to-the-refresh-token-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!