laravel 发布http://localhost:8000/broadcasting/auth 403(禁止)

bmvo0sr5  于 2023-02-10  发布在  其他
关注(0)|答案(5)|浏览(207)

我正在尝试让我的应用程序连接到一个私人频道上的推送器。
但我在控制台中收到以下错误:
POST http://localhost:8000/broadcasting/auth 403(禁止)
app.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.component('payment', require('./components/Payment.vue'));
Vue.component('form-ajax', require('./components/FormAjax.vue'));
Vue.component(
    'passport-clients',
    require('./components/passport/Clients.vue')
);

Vue.component(
    'passport-authorized-clients',
    require('./components/passport/AuthorizedClients.vue')
);

    Vue.component(
    'passport-personal-access-tokens',
    require('./components/passport/PersonalAccessTokens.vue')
);

const app = new Vue({
    el: '#app'
});

Echo.private(`articles.admin`)
    .listen('ArticleEvent', function(e)  {
        console.log(e);
    });

Error
错误的原因可能是什么以及如何解决它。

nx7onnlm

nx7onnlm1#

错误403 /broadcasting/auth与Laravel版本〉5.3和Pusher,您需要更改您的代码在资源/资产/js/bootstrap.js

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'your key',
    cluster: 'your cluster',
    encrypted: true,
    auth: {
        headers: {
            Authorization: 'Bearer ' + YourTokenLogin
        },
    },
});

并在app/提供商/广播服务提供商.php中更改

Broadcast::routes()

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

Broadcast::routes(['middleware' => ['jwt.auth']]); //if you use JWT

对我很有效,希望对你有帮助。

ghg1uchk

ghg1uchk2#

您是否尝试过自定义authEndpoint。
这东西在我这边管用。
bootsrap.js

window.Echo = new Echo({
    broadcaster: 'pusher',
    // ...
    authEndpoint: '/custom/endpoint/auth'
});
pftdvrlh

pftdvrlh3#

在我的情况下,我使用了一个自定义的auth保护,这是导致问题。
我已经添加了中间件来通过我的自定义auth guard,这就解决了这个问题。

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

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

This链接详细解释了正在发生的事情。

bis0qfac

bis0qfac4#

我添加了下面的代码到routes/web.php和它的工作。

Route::post('/broadcasting/auth', function () {
    return Auth::user();
 });
vfwfrxfs

vfwfrxfs5#

我也遇到过同样的问题,这一招保存我的命。
转到API

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

相关问题