Skip to content

Add authorization headers #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ window.Echo = new Echo({
broadcaster,
// replace the placeholders
host: 'wss://{api-ip}.execute-api.{region}.amazonaws.com/{stage}',
authEndpoint: '{auth-url}/broadcasting/auth', // Optional: Use if you have a separate authentication endpoint
bearerToken: '{token}', // Optional: Use if you need a Bearer Token for authentication
});
```

Expand Down
6 changes: 6 additions & 0 deletions js-src/Websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,15 @@ export class Websocket {
if (channel.name.startsWith('private-') || channel.name.startsWith('presence-')) {
console.log(`Sending auth request for channel ${channel.name}`)

if (this.options.bearerToken) {
this.options.auth.headers['Authorization'] = 'Bearer ' + this.options.bearerToken;
}

axios.post(this.options.authEndpoint, {
socket_id: this.getSocketId(),
channel_name: channel.name,
}, {
headers: this.options.auth.headers || {}
}).then((response: AxiosResponse) => {
console.log(`Subscribing to channels ${channel.name}`)

Expand Down
Loading