Reputation: 951
While opening Web Socket I am asked to type my login and password, but at this point I am already logged in. How can I make my app pass credentials while opening Web socket automatically? I am only using basic authentication but I can't find any info where to put Authorization header (or any different solution to my problem).
This pops up while opening Web Socket (basic log in form but in polish):
Code for opening web socket:
initializeWebSocketConnection(lobbyName: string): void {
const ws = new SockJS(this.addressStorage.apiAddress + '/socket');
this.stompClient = Stomp.over(ws);
// this.stompClient.debug = null;
const that = this;
this.stompClient.connect({}, function () {
that.stompClient.subscribe('/lobby/' + lobbyName, (message) => {
if (message.body) {
console.log('socket');
}
});
});
}
EDIT:
After I have added header to connect()
function I still need to login through popup form. Am I missing something or doing it wrong?
changed code:
initializeWebSocketConnection(lobbyName: string): void {
const ws = new SockJS(this.addressStorage.apiAddress + '/socket');
this.stompClient = Stomp.over(ws);
// this.stompClient.debug = null;
const that = this;
const headers = {
'authorization': this.authManager.basicToken
};
this.stompClient.connect(headers, function () {
that.stompClient.subscribe('/lobby/' + lobbyName, (message) => {
if (message.body) {
console.log('socket');
}
});
});
}
Upvotes: 0
Views: 645
Reputation: 845
The first argument of the connect() function is the header object. You can add the Authorization in.
https://stomp-js.github.io/stomp-websocket/codo/extra/docs-src/Usage.md.html
Upvotes: 1