Laravel Broadcast-结合多个中间件(网络,auth:api) [英] Laravel Broadcast - Combining multiple middleware (web, auth:api)

查看:288
本文介绍了Laravel Broadcast-结合多个中间件(网络,auth:api)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel Event Broadcast和Pusher在我的API和Web上同时使用websocket.如果我单独尝试,它们都可以正常工作.我的意思是:

I am using Laravel Event Broadcast and Pusher to utilize websockets both on my API and Web. If I try them individually, both work fine. What I mean is:

Broadcast::routes(['middleware' => 'web']); // this works for my Laravel website

Broadcast::routes(['middleware' => 'auth:api']); // this works for my api

但是,如果我想像这样同时使用两者:

However, if I want to use both at the same time like this:

Broadcast::routes(['middleware' => ['auth:api', 'web']]); // doesn't work

...两者均崩溃,我怀疑这是假设我正在尝试同时启用两个auth:api && web中间件.

... it crashes for both, which I suspect that it is assuming I am trying to enable for both auth:api && web middlewares.

是否有一种方法可以对此使用OR类型的语句(auth::api || 'web')?如果我想同时使用两者,并且如果它通过了一个中间件,它将绕过中间件怎么办?

Is there a way to use an OR kind of statement for this (auth::api || 'web')? What if I want to use both at the same time and if it passes one middleware, it bypasses the middleware.

请注意,我的API使用Laravel Passport.

Please note that I am using Laravel Passport for my api.

或者是否有一种方法可以为两者组合并创建混合的中间件(本质上将检查api或web)?所以我可以使用类似这样的东西:

Or is there a way to combine and creating a mixed middleware for both (which will essentially check for either api or web)? So I can use something like this maybe:

Broadcast::routes(['middleware' => 'broadcast']); // or auth:broadcast


更新:

据我了解,如果创建一个名为broadcast的新中间件,则可以:

As far as I understand, if I create a new Middleware called broadcast, I can do:

class BroadcastMiddleware() {

  public function handle() {
    $web = Auth::guard('web')->user();
    if ($web) {
        return response()->json($web);
    }

    $api = Auth::guard('api')->user();
    if ($api) {
        return response()->json($api);
    }
    return response()->json('Unauthorized.', 500);
  }
}

但是如何更改/broadcasting/auth路线?如果我尝试这样做:

But then how do I change /broadcasting/auth route? If I try this:

Route::post('/realtime/auth', function(){
    return true;
})->middleware('broadcast');

这将返回用户对象信息,但是,它应该返回如下内容:auth:"374f7ff42b7da877aa35:c29addedec281b418331a61dc3cfc74da8b108222565fa4924a8..."

This returns the user object info, however instead, it should return something like: auth:"374f7ff42b7da877aa35:c29addedec281b418331a61dc3cfc74da8b108222565fa4924a8..."

推荐答案

我终于弄清楚了该怎么做.

I finally figured out how to do it.

我不确定这是否是实现此目标的最佳方法,我非常感谢您所做的任何改进.

我如何实现的是为"web"创建了一个新的中间件,而将另一个保持不变.步骤如下.

How I achieved is created a new middleware for 'web' and left the other one as it it. Here are the steps.

1)在"BroadcastServiceProvider"中,仅将auth:api保护保留为Broadcast::routes(['middleware' => 'auth:api']);.

1) In 'BroadcastServiceProvider', left only auth:api guard for Broadcast::routes(['middleware' => 'auth:api']);.

这样,用于验证广播的Laravel的auth:api方法可以按预期工作.

This way, Laravel's auth:api method for authenticating broadcasting works as expected.

2)创建了一个名为广播"的中间件,并将其映射到Kernel.php中,如下所示:

2) Created a middleware called "Broadcast" and mapped it in Kernel.php like so:

'broadcast' => \App\Http\Middleware\Broadcast::class

Broadcast.php中间件如下:

public function handle($request, Closure $next)
{
    $web = Auth::guard('web')->user();
    if ($web) {
        return response()->json(\Illuminate\Support\Facades\Broadcast::auth($request));
    }

    return response()->json('Unauthorized.', 500);
}

3)在我的路线> web.php

3) Created a unique route other than Laravel's /broadcasting/auth in my routes>web.php

Route::post('/guard/broadcast/auth', function(\Illuminate\Support\Facades\Request $req){
    return true;
})->middleware('broadcast');

4)然后仅在刀片上使用,就像这样:

4) And then only on my blade, I use it like so:

<script>

let pusher = new Pusher("{{ env('PUSHER_APP_KEY') }}", {
    cluster: 'us2',
    encrypted: true,
    auth: {
        headers: {
            'X-CSRF-TOKEN': "{{ csrf_token() }}"
        }
    },
    authEndpoint: '{{ env('APP_URL') }}' + '/guard/broadcast/auth',
});

let channel = pusher.subscribe('private-channel.{{ Auth::user()->id }}');

channel.bind('my-event', addMessage);

function addMessage(data) {
    console.log(data);
}

</script>

这篇关于Laravel Broadcast-结合多个中间件(网络,auth:api)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆