Laravel / broadcasting / auth返回(找到302)并重定向到登录页面

时间:2018-11-19 06:15:38

标签: laravel-5 pusher

我有一个自定义用户App\Models\Client,我想为其广播通知,Pusher在我的应用中可以与公共频道配合使用,但不能与私人频道一起使用。

因此问题是/broadcasting/auth无法真正验证当前用户。

App\Providers\BroadcastServiceProvider.php

public function boot()
{
    Broadcast::routes(['middleware' => ['auth:client']]);

    Broadcast::channel('App.Models.Client.{id}', function ($user, $id) {
        return true;
    });
}

bootstrap.js

window.Pusher = require('pusher-js');
import Echo from "laravel-echo";

Pusher.logToConsole = true;

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: '***********',
    cluster: 'us2',
    encrypted: true,

    //auth: {
    //    headers: {
    //        'X-CSRF-TOKEN': token.content // already set in the request by axios
    //    }
    //},
});

if (Laravel.clientId) {

    window.Echo.private(`App.Models.Client.${Laravel.clientId}`)
        .notification((notification) => {
            console.log(notification);
        });
}

我正在使用普通的Laravel会话,因此不需要auth:api中间件或Authorization标头。 X-CSRF-TOKEN包含在请求中。

/broadcasting/auth请求/响应屏幕截图here

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:0)

Laravel 5.8

Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: <Widget>[
    TextField(
      controller: _controller,
      decoration: InputDecoration(prefixText: "https://"),
    ),
    RaisedButton(
      child: Text("Submit"),
      onPressed: () {
        String text = _controller.text.toString();
        if (!text.contains("https://")) {
          text = "https://" + text;
        }
        // text here will always have https://
      },
    ),
  ],
),
app/Providers/BroadcastServiceProvider.php
public function boot()
{
   Broadcast::routes([ 'middleware' => [ 'web', 'isLoginFrontend']]);
   require base_path('routes/channels.php');
}
routes/channels.php

答案 1 :(得分:0)

改成这样

Broadcast::routes(['middleware' => ['web', 'auth:client']]);

App\Providers\BroadcastServiceProvider.php:

我在我的项目中得到了这个。

答案 2 :(得分:0)

在except数组中添加/broadcasting/auth(VerifyCsrfToken类)

相关问题