首页 文章

Laravel / broadcast / auth总是因403错误而失败

提问于
浏览
4

我最近钻研了Laravel 5.3的Laravel-Echo和Pusher组合 . 我已经成功 Build 了公共 Channels ,然后转向私人 Channels . 我在Laravel从/ broadcast / auth路由返回403时遇到了麻烦,无论我做什么尝试授权操作(包括使用简单的返回true语句) . 谁能告诉我我做错了什么?

应用/供应商/ BroadcastServiceProvider.php:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Broadcast;

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Broadcast::routes();

        /*
         * Authenticate the user's personal channel...
         */
        Broadcast::channel('App.User.*', function ($user, $userId) {
            return true;
        });
    }
}

resources/assets/js/booststrap.js:
 import Echo from "laravel-echo"

 window.Echo = new Echo({
     broadcaster: 'pusher',
     key: 'My-Key-Here'
 });

window.Echo.private('App.User.1')
    .notification((notification) => {
        console.log(notification.type);
    });

我可以在我的Pusher调试控制台中看到事件和它的有效负载,一旦它到达auth路由就会失败 .

7 回答

  • 1

    错误403 /使用Laravel版本> 5.3&Pusher进行广播/验证,您需要更改资源/ assets / js / bootstrap.js中的代码

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

    并在app / Providers / BroadcastServiceProvider.php中更改

    Broadcast::routes()
    

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

    要么

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

    它对我有用,希望对你有所帮助 .

  • 3

    我通过创建 Channels 路线来解决它 .

    在routes-> channels.php中创建授权通道

    Broadcast::channel('chatroom', function ($user) {
        return $user;
    });
    

    请参阅文档:https://laravel.com/docs/5.4/broadcasting#authorizing-channels

    谢谢

  • 0

    对我有用的是使用Laravel Echo包的方法 privatehttps://laravel.com/docs/5.3/notifications#listening-for-notifications

    Echo.private('App.User.1')
      .notification((notification) => {
      console.log(notification.type);
    });
    
  • 0

    在我的情况下,问题是一个错误的用户ID:

    Echo.private('user.'+CURRENT_USER_ID_HERE)
    
  • 0

    如果您不再登录,可能会发生这种情况 . 请确保您实际登录到Laravel应用程序并且当前会话尚未过期 .

    我重新登录,它对我有用 .

  • 0

    如果有人在最新的Laravel 5.7中遇到同样的问题,那么对我来说很好的解决方案是在返回之前或在返回时检查每个通道中的身份验证,如下所示 .

    Broadcast::channel('user.*', function ($user) {
    return Auth::check();
    

    });

    Broadcast::channel('conversation.{id}', function ($user, $conversationId) {
            Auth::check();
        return $user->isInConversation(Conversation::find($conversationId));
    });
    

    这种方式适用于任何包括用户在内的任何活动的 Channels .

    我希望它可以帮助别人 .

  • 0

    检查您如何授权您的 Channels . 根据您的设置,这可能会有所帮助 . 使用以下内容更新您的BroadcastServiceProvider:

    <?php
    
    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    use Illuminate\Support\Facades\Broadcast;
    
    class BroadcastServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            Broadcast::routes(['middleware' => ['auth:api']]);
    
            require base_path('routes/channels.php');
        }
    }
    

    添加Auth API中间件以与Laravel Passport一起使用 .

相关问题