Search code examples
botframeworkdirect-line-botframework

Store refresh token in a cookie


I am initializing the chat through a token obtained calling to DirectLine Api (POST/v3/directline/tokens/generate). I store this token in a cookie and use it to resume a conversation. My problem is that the direct line automatically refreshes the token after 30 minutes and I didn't find a way to save the new token. Does anyone know how I can save the new token?

Thanks in advance


Solution

  • Subscribing to conectionStatus of DirectLine enables listening for ConnectionStatus.Online which is fired after ConnectionStatus.ExpiredToken. Then, update the cookie:

    directLine.connectionStatus$.subscribe(connectionStatus => {
        if (connectionStatus === ConnectionStatus.Online) {
          setTokenCookie(this.token);
        }
      });
    

    Reference DirectLineJs source: https://github.com/Microsoft/BotFramework-DirectLineJS/blob/master/src/directLine.ts#L473

     public reconnect(conversation: Conversation) {
            this.token = conversation.token;
            this.streamUrl = conversation.streamUrl;
            if (this.connectionStatus$.getValue() === ConnectionStatus.ExpiredToken)
                this.connectionStatus$.next(ConnectionStatus.Online);
        }