question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Updating the Authorization-header

See original GitHub issue

So I have a single page application with authorization setup. When the app boots, it will check if the user is authenticated. If not, the user is send to the login page, if so, the app sets the Authorization header of the requests to the token saved in localStorage. This is working fine, but when I want to login and update the Authorization token, the update isn’t reflected and I have to manually refresh the page to make the request with the token.

As you can see here, I set the token correctly: https://github.com/petervmeijgaard/jwt-example/blob/next/resources/frontend/src/app/store/modules/auth/mutations.js#L24

And even when I do a console.log(Vue.echo.options.auth.headers.Authorization) before I subscribe to a private channel, I get the correct token returned. However, when I check the request send to the REST-server, the Authorization header isn’t set.

The console.log ouput: The console.log output

The request headers: The request headers

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:28 (1 by maintainers)

github_iconTop GitHub Comments

47reactions
kubacodecommented, Sep 20, 2016

@petervmeijgaard I think I’ve figured it out. The Authorization header isn’t sent by Echo, its sent by Pusher. Setting the Authorization header in Echo only sets the header when the Pusher instance is constructed.

To set the Authorization header on the fly, you need to set the header on the Pusher object:

Echo.connector.pusher.config.auth.headers['Authorization'] = 'Bearer token';

I’m using the vue-auth plugin, so I’m able to add the header by overriding the _setHeaders function. You should also be able to set the header via a vue-resource interceptor.

10reactions
rgreer4commented, Jul 9, 2017

Setting the pusher config directly did not work for me (and I’m not using vue-resource) – but this solution worked: https://github.com/tlaverdure/laravel-echo-server/issues/129#issuecomment-284216291

In my case I’m, using vue-echo on top of Echo, but the config block should work directly in Echo as well. For example:

           Vue.use(VueEcho, {
                broadcaster: 'pusher',
                key:         PUSHER_KEY,
                auth:        {
                    headers: {
                        Authorization: 'Bearer ' + BEARER_TOKEN,
                    },
                },
            });

…and setting:

Broadcast::routes(['middleware' => ['auth:api']]);

…in BroadcastServiceProvider as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authorization - HTTP - MDN Web Docs - Mozilla
The HTTP Authorization request header can be used to provide credentials that authenticate a user agent with a server, allowing access to a ......
Read more >
How to update existing Authorization Header for all future ...
I am creating a refresh token mechanism that uses a custom middleware to check if the current token is still valid by calling...
Read more >
Configure CloudFront to forward the Authorization header to ...
Under Headers, choose Include the following headers. Then, under Add Headers, select Authorization. Choose Save changes. Did this article help?
Read more >
Update Authorization header in requests to Scanner/Repeater
Hi, TL;DR: Trying to update subsequent requests with updated session information after session times out (in scanner/repeater) - Basically won't ...
Read more >
Updating the Authorization header with Observables - Devpost
Updating the Authorization header with Observables - Learn how to subscribe to changes of a value from any React component using the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found