【问题标题】:Laravel-echo showing 401 (Unauthorized) for private channel except one userLaravel-echo 显示 401(未经授权)的私人频道,除了一位用户
【发布时间】:2021-10-17 12:13:39
【问题描述】:

我从 reactJS 获得 laravel 私人频道的 401(未授权)状态。

这适用于 userId=1,但为所有其他用户返回 401。

这是 Laravel 的代码

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Broadcast::routes([
            "middleware" => ['api', 'jwt.auth'],
            "prefix" => "api"
        ]);

        require base_path('routes/channels.php');
    }
}


class AppointmentEvent implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $appointment;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($appointment)
    {
        $this->appointment = $appointment;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('App.User.' . $this->appointment->patient_id);
    }
}


Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

这是反应代码

const token = JSON.parse(localStorage.getItem("patientToken"));
const options = {
    broadcaster: 'pusher',
    key: '1c**************61',
    cluster: 'ap2',
    encrypted: true,
    authEndpoint: 'http://localhost/heal/api/broadcasting/auth', 
    auth: {
      headers: {
        Authorization: `Bearer ${token}`,
        Accept: 'application/json',
      },
    },
  };
  window.Echo = new Echo(options);

var p = localStorage.getItem("patientId");
      window.Echo.private(`App.User.${p}`).listen('ConfirmAppointmentEvent', (e) => {
        alert("Doctor accepted your appointment")
        console.log(e)
      });

非常适合 user_id 1,但不适用于其他人 我确实取消了App\Providers\BroadcastServiceProvider::class,的注释

Laravel 版本 7.30.0

【问题讨论】:

    标签: reactjs laravel pusher laravel-echo laravel-broadcast


    【解决方案1】:

    为我工作

    AuthEndpointController

        public function auth(Request $request)
        {
            $pusher = new Pusher(env('PUSHER_APP_KEY'), env('PUSHER_APP_SECRET'), env('PUSHER_APP_ID'));
            if($request->request->get('channel_name') === 'private-App.User.' . $request->user()->id) {
                return $pusher->socket_auth($request->request->get('channel_name'), $request->request->get('socket_id'));
            }
            return response()->json([], 400);
       }
    

    const token = JSON.parse(localStorage.getItem("patientToken"));
    const options = {
      broadcaster: 'pusher',
      key: '1c4c1546ef4c02340c61',
      cluster: 'ap2',
      forceTLS: true,
      authEndpoint: 'http://localhost/heal/api/broadcasting/auth', 
      auth: {
        headers: {
          Authorization: `Bearer ${token}`,
        },
      },
    };
    window.Echo = new Echo(options);
    var p = localStorage.getItem("patientId");
      window.Echo.private(`App.User.${p}`).listen('ConfirmAppointmentEvent', (e) => {
        alert("Doctor accepted your appointment")
        console.log(e)
      });

    【讨论】:

    猜你喜欢
    • 2019-07-03
    • 1970-01-01
    • 2020-11-22
    • 2020-07-23
    • 2017-06-01
    • 2020-01-15
    • 2020-11-15
    • 2021-03-09
    • 1970-01-01
    相关资源
    最近更新 更多