@experteam-mx/ngx-services 20.8.8 → 20.8.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5582,11 +5582,14 @@ var DocumentStatusCode;
5582
5582
  DocumentStatusCode[DocumentStatusCode["WITH_ERRORS"] = 3] = "WITH_ERRORS";
5583
5583
  })(DocumentStatusCode || (DocumentStatusCode = {}));
5584
5584
 
5585
+ const SOCKETS_AUTH_PATH = '/sockets/auth';
5585
5586
  class WebSocketsService {
5586
5587
  pusher = null;
5587
5588
  appKey = null;
5588
5589
  connectPromise = null;
5589
5590
  environments = inject(ENVIRONMENT_TOKEN);
5591
+ getAuthToken = null;
5592
+ channelAuthHeaders = {};
5590
5593
  /**
5591
5594
  * Stores the Pusher/Soketi application key without opening a connection.
5592
5595
  * The first `channelSub` / `eventBind` opens Pusher via `ensureConnected`.
@@ -5596,6 +5599,15 @@ class WebSocketsService {
5596
5599
  setAppKey(appKey) {
5597
5600
  this.appKey = appKey;
5598
5601
  }
5602
+ /**
5603
+ * Registers a live token provider used when authorizing private/presence channels.
5604
+ * The provider is called at subscribe/authorize time; the token is not snapshotted here.
5605
+ *
5606
+ * @param {() => string | null} getToken - Returns the current session token, or null.
5607
+ */
5608
+ setAuthTokenProvider(getToken) {
5609
+ this.getAuthToken = getToken;
5610
+ }
5599
5611
  /**
5600
5612
  * Connects to Pusher using the given app key and env `sockets` url/port/debug.
5601
5613
  * No-ops if already connected with the same key; reconnects if the key changed.
@@ -5618,13 +5630,27 @@ class WebSocketsService {
5618
5630
  this.pusher.disconnect();
5619
5631
  this.pusher = null;
5620
5632
  }
5621
- const pusher = new Pusher(appKey, {
5633
+ const options = {
5622
5634
  wsHost: sockets.url,
5623
5635
  wsPort: sockets.port,
5624
5636
  enabledTransports: ['wss', 'ws'],
5625
5637
  forceTLS: false,
5626
5638
  enableStats: false,
5627
- });
5639
+ };
5640
+ const { apiSecurityUrl } = this.environments;
5641
+ if (apiSecurityUrl) {
5642
+ this.refreshChannelAuthHeader();
5643
+ options.channelAuthorization = {
5644
+ transport: 'ajax',
5645
+ endpoint: `${apiSecurityUrl}${SOCKETS_AUTH_PATH}`,
5646
+ headers: this.channelAuthHeaders,
5647
+ headersProvider: () => {
5648
+ this.refreshChannelAuthHeader();
5649
+ return this.channelAuthHeaders;
5650
+ },
5651
+ };
5652
+ }
5653
+ const pusher = new Pusher(appKey, options);
5628
5654
  if (sockets.debug) {
5629
5655
  Pusher.logToConsole = true;
5630
5656
  }
@@ -5653,12 +5679,14 @@ class WebSocketsService {
5653
5679
  */
5654
5680
  async channelSub(channelName) {
5655
5681
  const pusher = await this.ensureConnected();
5682
+ this.refreshChannelAuthHeader();
5656
5683
  const attempts = 3;
5657
5684
  for (let attempt = 0; attempt <= attempts; attempt++) {
5658
5685
  const channel = pusher.channel(channelName);
5659
5686
  if (channel?.subscribed) {
5660
5687
  return channel;
5661
5688
  }
5689
+ this.refreshChannelAuthHeader();
5662
5690
  pusher.subscribe(channelName);
5663
5691
  if (attempt < attempts) {
5664
5692
  await this.wait();
@@ -5679,6 +5707,7 @@ class WebSocketsService {
5679
5707
  void (async () => {
5680
5708
  try {
5681
5709
  const pusher = await this.ensureConnected();
5710
+ this.refreshChannelAuthHeader();
5682
5711
  if (channel?.name) {
5683
5712
  channel.bind(event, (data) => subscriber.next(data));
5684
5713
  }
@@ -5723,6 +5752,15 @@ class WebSocketsService {
5723
5752
  throw error;
5724
5753
  }
5725
5754
  }
5755
+ refreshChannelAuthHeader() {
5756
+ const token = this.getAuthToken?.() ?? null;
5757
+ if (token) {
5758
+ this.channelAuthHeaders['Authorization'] = `Bearer ${token}`;
5759
+ }
5760
+ else {
5761
+ delete this.channelAuthHeaders['Authorization'];
5762
+ }
5763
+ }
5726
5764
  async wait() {
5727
5765
  const waitTime = 3 * 1000;
5728
5766
  return new Promise((resolve) => setTimeout(resolve, waitTime));