0

I'm trying to implement chat using Angular, Laravel and Pusher. The chat is working but now i want to auth the user in order to have indication if the user is online or not. I understood that in order to do this i need to make private rooms instead of public rooms, When i'm trying to make auth for the users i'm getting 419 unknown status.

Angular Code:

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

import { UserSessionService } from 'app/shared/_services/user-session.service';

import { ChatMessage } from 'app/shared/_models/chat-message.model';
import { environment } from 'environments/environment';

declare const Pusher: any;

@Injectable()
export class BroadcastService {

  readonly pusherKey = environment.pusherKey;

  private channels = {};

  newChatMessage = new Subject<ChatMessage>();
  activeChanel = new Subject<any>();

  constructor(private userSession: UserSessionService) {}

  openChatSocket(chatId: number): void {
    if (this.channels[chatId]) {
      return;
    }

    const pusher = new Pusher(this.pusherKey, {
      cluster: 'eu',
      encrypted: true,
      authEndpoint: environment.apiUrl + '/broadcasting/auth',
      auth: {
        headers: {
          'Authorization': this.userSession.getToken()
        }
      }
    });

    this.channels[chatId] = pusher.subscribe('private-chat-' + chatId);
    this.activeChanel.next(this.channels[chatId])
    this.channels[chatId].bind('new-message', fullMessageInfo => {
      this.newChatMessage.next(fullMessageInfo.message);
    });
  }

  closeChatSockets(): void {
    Object.keys(this.channels).forEach(channel => {
      this.channels[channel].unsubscribe();
    });

    this.channels = {};
  }
}

I already tried to exclude http://localhost:4200 in VerifyCsrfToken.php and it didn't work.

4

0 回答 0