Skip to content

Commit

Permalink
Add custom news headers
Browse files Browse the repository at this point in the history
  • Loading branch information
ezequidias committed May 26, 2023
1 parent 9e93737 commit d5102f1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/channel-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface AuthRequest {
response: Promise<void>;
}

export function authRequest(channel: string, connection: EventSourceConnection, authEndpoint = '/broadcasting/auth'): AuthRequest {
export function authRequest(channel: string, connection: EventSourceConnection, options: any): AuthRequest {
let authorized = false;
let afterAuthCallbacks: Function[] = [];

Expand All @@ -23,7 +23,7 @@ export function authRequest(channel: string, connection: EventSourceConnection,
return this;
}

const response = request(connection).post(authEndpoint, { channel_name: channel }).then((response) => {
const response = request(connection).post(options.authEndpoint, { channel_name: channel }, options).then((response) => {
authorized = true;

afterAuthCallbacks.forEach((callback) => callback(response));
Expand Down
6 changes: 3 additions & 3 deletions src/echo-broadcaster/wave-presence-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class WavePresenceChannel extends WavePrivateChannel implements P
constructor(connection, name, options) {
super(connection, name, options);

this.joinRequest = authRequest(name, connection, this.options.endpoint + '/presence-channel-users')
this.joinRequest = authRequest(name, connection, { ...this.options, endpoint: this.options.endpoint + '/presence-channel-users' })
.after(() => this.joined = true);

if (typeof window !== 'undefined') {
Expand All @@ -23,7 +23,7 @@ export default class WavePresenceChannel extends WavePrivateChannel implements P
public here(callback: Function): WavePresenceChannel {
if (this.joined) {
request(this.connection)
.get(this.options.endpoint + '/presence-channel-users', { channel_name: this.name })
.get(this.options.endpoint + '/presence-channel-users', { channel_name: this.name }, this.options)
.then((users) => callback(users));

return this;
Expand Down Expand Up @@ -54,7 +54,7 @@ export default class WavePresenceChannel extends WavePrivateChannel implements P

public unsubscribe(): void {
this.joinRequest.after(() => {
request(this.connection).delete(this.options.endpoint + '/presence-channel-users', { channel_name: this.name });
request(this.connection).delete(this.options.endpoint + '/presence-channel-users', { channel_name: this.name }, this.options);
super.unsubscribe();
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/echo-broadcaster/wave-private-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export default class WavePrivateChannel extends WaveChannel {
constructor(connection, name, options) {
super(connection, name, options);

this.auth = authRequest(name, connection, this.options.authEndpoint);
this.auth = authRequest(name, connection, this.options);
}

public whisper(eventName, data) {
request(this.connection).post(this.options.endpoint + '/whisper', { channel_name: this.name, event_name: eventName, data });
request(this.connection).post(this.options.endpoint + '/whisper', { channel_name: this.name, event_name: eventName, data }, this.options);

return this;
}
Expand Down
19 changes: 17 additions & 2 deletions src/echo/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ export abstract class Connector {
headers: {},
},
authEndpoint: '/broadcasting/auth',
userAuthentication: {
endpoint: '/broadcasting/user-auth',
headers: {},
},
broadcaster: 'pusher',
csrfToken: null,
bearerToken: null,
host: null,
key: null,
namespace: 'App.Events',
Expand All @@ -35,8 +40,18 @@ export abstract class Connector {
protected setOptions(options: any): any {
this.options = Object.assign(this._defaultOptions, options);

if (this.csrfToken()) {
this.options.auth.headers['X-CSRF-TOKEN'] = this.csrfToken();
let token = this.csrfToken();

if (token) {
this.options.auth.headers['X-CSRF-TOKEN'] = token;
this.options.userAuthentication.headers['X-CSRF-TOKEN'] = token;
}

token = this.options.bearerToken;

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

return options;
Expand Down
7 changes: 5 additions & 2 deletions src/util/request.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { EventSourceConnection } from "../EventSourceConnection";

export default function request(connection: EventSourceConnection) {
function create(method: 'GET' | 'POST' | 'PUT' | 'DELETE', route: string, data?: object): Promise<Response> {
function create(method: 'GET' | 'POST' | 'PUT' | 'DELETE', route: string, data?: object, options?: any): Promise<Response> {
let csrfToken = null;
if (typeof document !== 'undefined') {
const match = document.cookie.match(new RegExp('(^|;\\s*)(XSRF-TOKEN)=([^;]*)'));
csrfToken = match ? decodeURIComponent(match[3]) : null;
}

const tokenBearer = options.bearerToken ? {'Authorization': 'Bearer '+options.bearerToken} : {}
const fetchRequest = (connectionId) => fetch(route, {
method: method,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Socket-Id': connectionId,
'X-XSRF-TOKEN': csrfToken,
...options?.auth?.headers,
...tokenBearer
},
body: JSON.stringify(data || {}),
}).then((response) => {
Expand All @@ -28,7 +31,7 @@ export default function request(connection: EventSourceConnection) {
}) : fetchRequest(connection.getId());
}

const factory = (method: 'GET' | 'POST' | 'PUT' | 'DELETE') => (route: string, data?: object) => create(method, route, data);
const factory = (method: 'GET' | 'POST' | 'PUT' | 'DELETE') => (route: string, data?: object, options?: any) => create(method, route, data, options);

return {
get: factory('GET'),
Expand Down
2 changes: 1 addition & 1 deletion src/wave-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class WaveModel {

const channelName = `${this.options.namespace}.${this.name}.${this.key}`;

this.auth = authRequest(channelName, connection, this.options.authEndpoint);
this.auth = authRequest(channelName, connection, this.options);

this.channel = `private-${channelName}`;

Expand Down

0 comments on commit d5102f1

Please sign in to comment.