How to set authorization header in ng2-signalr?

I use the ng2-signalr in ionic version 2. The problem is that I do not know how to set the authorization header. I have a search but have not found any example.

My code to connect to the server.

  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 title?

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

Library: ng2-signalr

Update 1

Here's the link for the Query String Solution . The workaround is to pass authorization token to qs and handle it accordingly. But I want to set the title so that this solution does not suit me. Another reason when I have one other client (Simple angularjs signalr) that works fine when I set the header as shown below.

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

Note. Before implementing authorization, the same code works fine, since I do not need to set the authorization header.

+5
source share
1 answer

Currently (v2.0.4) is 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. }); 
+1
source

Source: https://habr.com/ru/post/1268384/


All Articles