Typescript Servicestack Client Authentication for SSE Events

I am trying to use typescript servicestack-client to connect to SSE events from ServiceStack server.

Authentication is done by sending the class Authenticate

and receiving the sesssion cookies:

import { ServerEventsClient } from 'servicestack-client';

export class ServicestackService {
    private sseClient: ServerEventsClient;

    constructor() {
        this.createSseClient();
    }

    login(username: string, password: string) {
        let request = new Authenticate(username, password);
        return this.sseClient.serviceClient.post(request);
    }

    startClient() {
        this.sseClient.start();
        console.log('eventSource created');
    }

    subscribeChannel(channel: string) {
        this.sseClient.subscribeToChannels(channel);
    }

    private createSseClient() {
        this.sseClient = new ServerEventsClient('http://ss_api_url', ['*'], {
        handlers: { ...define handlers for onConnect & onMessage here... }
        }
    }

}    

      

The above code successfully authenticates the API servicestack and the secure API requests work well. The method startClient()

is called after login authentication is complete and creates an object eventSource

as a property in this.sseClient

.

But this object has withCredentials: false

! Therefore, any of the following channel subscriptions fail to authenticate because the client servicestack does not send the header Cookies

.

How can I make an authenticated SSE connection?

+3


source to share


1 answer


I just added support for specifying the withCredentials

EventSource option in this commit , which is by default true

, so it will automatically be there if you upgrade to the latest v0.0.34 servicestack-client .

If necessary, you can disable it with



var sseClient = new ServerEventsClient('http://ss_api_url', ['*'], { ... });

sseClient.withCredentials = false;

      

+1


source







All Articles