Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Authorization Header in ng2-signalr?

I am using library ng2-signalrin ionic 2. Issue is that i don't know how to set authorization header. I have search but didn't find any example.

My code for connecting to server hub.

   let options: IConnectionOptions = { qs:{userId:1}, url: "http://192.168.0.211:44337"};

        console.log("Stage 1");
            //Header for 'this.singalR.connect'
        this.signalR.connect(options)
            .then((connection) => {                      

                console.log("Client Id: " + connection.id);                     
             }, (err) => {
                console.log("SignalR Error: " + JSON.stringify(err));
            });

How to set below header ?

  var headers = new Headers({
            'Content-Type': "application/json",
            "Authorization": 'Bearer ' + accessToken  //accessToken contain bearer value.
        });

Library: ng2-signalr

Update 1

Here on link Query String Solution. A workaround is mention to pass authorization token in qs and handle accordingly. But i want to set in header so this solution does not suit me . One more reason as i have one another client(Simple angularjs signalr) which is working fine when i set header just like below.

 $.signalR.ajaxDefaults.headers = { Authorization: //here set header};  

Note: Before implementing Authorization same code working fine as i don't need to set authorization header.

like image 873
Usman lqbal Avatar asked Nov 08 '22 21:11

Usman lqbal


1 Answers

It is currently (v2.0.4) not implemented in ng2-signalr.
As a workaround, you can try:

declare var $: any;
$.signalR.ajaxDefaults.headers = new Headers({
        'Content-Type': "application/json",
        "Authorization": 'Bearer ' + accessToken  //accessToken contain bearer value.
}); 
like image 63
hannes neukermans Avatar answered Nov 15 '22 10:11

hannes neukermans