简体   繁体   English

Laravel广播身份验证未使用标准用户身份验证

[英]Laravel Broadcast Authentication Not Using Standard User Auth

I've spent a couple of days on this now and can not get my head around it. 我现在已经花了几天时间,无法理解。 I'm using this Laravel Package Ohmybrew/Laravel-Shopify which does not use the standard Laravel auth as they have their own middleware auth.shop 我正在使用此Laravel软件包Ohmybrew / Laravel-Shopify,它不使用标准的Laravel身份验证,因为它们具有自己的中间件auth.shop

The issue I am facing is because it does not use the $user variable as the default Auth is not there, broadcast authentication for WebSockets on private channels is always 403 not authorised. 我面临的问题是因为它不使用$ user变量,因为默认的Auth不存在,私有通道上的WebSockets广播身份验证始终是未经授权的403。

I've tried returning true usings another model, does not work. 我尝试使用其他模型返回true,但是不起作用。

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

I've also tried just returning true no matter what just to see. 我也尝试过只返回true,无论看到什么。 Also no luck. 也没有运气。

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

I've even messed about with the PusherBroadcast.php and BroadcastManager to remove throwing the not authenticated exception. 我什至把PusherBroadcast.php和BroadcastManager弄乱了,以消除抛出未经身份验证的异常。 This also does not work. 这也不起作用。

I'm still getting to grips with the way Laravel works as a whole package so not sure where to look next. 我仍然不了解Laravel作为一个整体的工作方式,因此不确定下一步该怎么看。 I've had a read up on custom auth guards but not sure that will work as the package has its own authentication. 我已经阅读了有关自定义身份验证防护的信息,但不确定该包是否具有其自身的身份验证功能,是否可以正常使用。

Any steer would be greatly appreciated as I am a little out of things to try. 任何转向都将不胜感激,因为我没有什么可以尝试的。 It seems anything I do to get onto a private channel does not work. 我进入私人频道的所有操作似乎均无效。

Note: It's worth mentioning a test public channel for listening to the WebSockets works fine with the current set up. 注意:值得一提的是,用于测试WebSockets的测试公共频道在当前设置下工作正常。 it is only the private channel authentication which is not working. 仅专用通道身份验证不起作用。

Update: Here is how I am listening to the channels 更新:这是我收听频道的方式

            window.Echo.private('shop.{{ ShopifyApp::shop()->id }}')
                .listen('ScheduleProcessed', function(e) {
                    console.log(e);
                });

            window.Echo.channel(`test`) // Broadcast channel name
                .listen('BroadcastTestEvent', (e) => { // Message name
                        console.log(e); // The operation performed by the message, the parameter e is the data carried
                        alert("GOT S**T");
                    }
                );

The second one for the test channel works completely fine. 测试通道的第二个完全正常。 The 'shop' channel does not and it's partly down to broadcasting/auth always returns 403 when the client tries to connect to the private channel. “商店”频道没有,部分原因是广播/身份验证在客户端尝试连接到专用频道时始终返回403。

广播身份验证失败

Update 2 Seems to have something to do with the fact the default auth needs to use the $request->user() which is empty as the custom auth does not use it at all. 更新2似乎与默认身份验证需要使用$request->user()为空有关,因为自定义身份验证根本不使用它。

I've read that binding the user might work but not 100% sure how. 我读过绑定用户可能有用,但不能100%确定如何绑定。

$request->merge(['user' => $user]);

//add this
$request->setUserResolver(function () use ($user) {
   return $user;
});

You can specify your own middleware for Broadcast routes. 您可以为广播路由指定自己的中间件。 Something like the following should work: 类似于以下内容的东西应该起作用:

Broadcast::routes(['middleware' => 'auth.shop']);

That should then use the Laravel-Shopify package to authenticate the user. 然后,应使用Laravel-Shopify软件包对用户进行身份验证。

If you are using sessions, you might also need to specify that you want to use the web middleware too. 如果您正在使用会话,则可能还需要指定您也要使用web中间件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM