davinkevin / AngularStompDK

Angular service to communicate to Stomp-Websocket
http://davinkevin.github.io/AngularStompDK/
Apache License 2.0
36 stars 12 forks source link

X-XSRF-TOKEN header - add it before connect but not in module config #50

Closed ioana-m closed 7 years ago

ioana-m commented 7 years ago

Could you please help. I am trying to add the X-XSRF-TOKEN token to headers. I get the token using $cookies service from angular, so I don't have access to it in module config only in run, and I cannot seem to find a way of changing the headers used on connect to stomp in module run.

This is what I wrote in module config: ngstompProvider.url('/ws').class(SockJS).debug(true).autoConnect(false);

How can I then set the header in module run, for this to work with spring CSRF websocket security: ngstomp.connect();

Thank you.

ioana-m commented 7 years ago

Hello, I was able to resolve my issue, by using the instance of ngstompProvider returned when url was set in config in the module run function. Very easy, son't know why I did't see it before. Thank you for the nice plugin.

davinkevin commented 7 years ago

I really like when issue are closed so fast 😄

Could you provide a code example of the solution, if someone want to do it ?

ioana-m commented 7 years ago

Sure here is what worked for me.


    var stompProvider = null;
    config.$inject = ['ngstompProvider'];

    function config(ngstompProvider) {
        stompProvider = ngstompProvider.url('/ws').class(SockJS).debug(true).autoConnect(false);
    }

    run.$inject = ['ngstomp', '$cookies'];

    function run(ngstomp, $cookies) {
        stompProvider.headers({
            'X-XSRF-TOKEN': $cookies.get("XSRF-TOKEN")
        });
        ngstomp.connect();
    }
davinkevin commented 7 years ago

What you've done is a good idea, but I think this should be done without using external variable.

You can also do this :

config.$inject = ['ngstompProvider'];
function config(ngstompProvider) {
    ngstompProvider.url('/ws').class(SockJS).debug(true).autoConnect(false);
}

run.$inject = ['ngstomp', '$cookies'];
function run(ngstomp, $cookies) {
    ngstomp.settings.headers({
        'X-XSRF-TOKEN': $cookies.get("XSRF-TOKEN")
    });
    ngstomp.connect();
}
ioana-m commented 7 years ago

Thank you for the suggestion, but I just tried it and it says. ngstomp.settings.headers is not a function.

davinkevin commented 7 years ago

I should have test my code example...😢

config.$inject = ['ngstompProvider'];
function config(ngstompProvider) {
    ngstompProvider.url('/ws').class(SockJS).debug(true).autoConnect(false);
}

run.$inject = ['ngstomp', '$cookies'];
function run(ngstomp, $cookies) {
    ngstomp.settings.headers = {
        'X-XSRF-TOKEN': $cookies.get("XSRF-TOKEN")
    };
    ngstomp.connect();
}

It should be better 😄

ioana-m commented 7 years ago

Yes, thank you, it works.