在SPA中授权广播频道 [英] Authorizing broadcasting channel in an SPA

查看:39
本文介绍了在SPA中授权广播频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目分为两个app:基于VUE的客户端app和基于laravel的服务器端rest API app。我已在config/app.php文件中取消注释AppProvidersBroadcastServiceProvider::class,

默认的广播授权路由为/broadcasting/auth。由于它应用了web中间件,因此由于CSRF问题,它显示为419。因此,在BroadcastServiceProvider中,我更改了以下内容:

Broadcast::routes();

至此:

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

但现在的问题是,每当我访问我的客户端应用程序时,我在控制台中都会收到以下错误:

GEThttp://localhost:8000/v1/login405(不允许使用方法)

如何修复此问题?

我的客户端配置:

window.Echo = new Echo({
    authEndpoint: 'http://localhost:8000/broadcasting/auth',
    broadcaster: 'pusher',
    key: 'anyKey',
    wsHost: window.location.hostname,
    wsPort: 6001,
    disableStats: true
});

window.Echo.private('test.1').listen('TestUpdated', (e) => {
    /*eslint-disable no-console*/
    console.log(e);
});

推荐答案

这就是我最终在api.php路由文件中执行的操作:

Route::post('/broadcast',function (Request $request){
    $pusher = new PusherPusher(env('PUSHER_APP_KEY'),env('PUSHER_APP_SECRET'), env('PUSHER_APP_ID'));
    return $pusher->socket_auth($request->request->get('channel_name'),$request->request->get('socket_id'));
});

然后我在客户端应用程序中将authEndpoint更改为该路线:

window.Echo = new Echo({
    authEndpoint: 'http://localhost:8000/broadcast',
    ...
}

这篇关于在SPA中授权广播频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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