hdw3
hdw3

Reputation: 951

How to automatically log in while opening web socket

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): popup img

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

Answers (1)

Kevin FONTAINE
Kevin FONTAINE

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

Related Questions