I am currently utilizing the library ng2-signalr
within my ionic 2 project. I am facing an issue regarding setting the authorization header, as I have been unable to find any examples on how to do so.
Below is my code snippet for establishing a connection with the 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));
});
My question is how to properly set the following header:
var headers = new Headers({
'Content-Type': "application/json",
"Authorization": 'Bearer ' + accessToken //accessToken contains bearer value.
});
Refer to the library documentation: ng2-signalr
Update 1
On this post, a workaround is suggested to pass the authorization token in the query string and handle it accordingly. However, I prefer to set it in the header as I need to follow a different approach due to another client (Simple AngularJS SignalR) working successfully when the header is set as shown below:
$.signalR.ajaxDefaults.headers = { Authorization: //set your header here};
Note: Prior to implementing the authorization header, the code was functioning without any issues.