SvenKirschbaum / react-stomp-hooks

This repository contain a react library which enables simple access to stomp subscriptions via hooks.
MIT License
63 stars 13 forks source link

How to pass username and password for the connection? #5

Closed yalamber closed 2 years ago

yalamber commented 3 years ago

what is correct way to pass username and password?

SvenKirschbaum commented 3 years ago

Hello!

In general that depends on the way your backend/broker expects you to authenticate. You can use the beforeConnect callback to set additional headers before the connection is made, e.g. like so:

const beforeStompConnect = useCallback(function () {
    this.connectHeaders = {
        'Authorization': "Bearer " + apitoken
    }
}, [apitoken]);

return (
    <StompSessionProvider
        url={`/api/sock/`}
        beforeConnect={beforeStompConnect}
    >
        ...
    </StompSessionProvider>
);
joannadal commented 2 years ago

Hello!

I am trying to add the 'Authorization' header into the connection on the StompSessionProvier. I have tried to use the code of your example, but the request to the server is not including the 'Authorization' header. Any idea how could I add it?

Thanks, Joan