php – Laravel / broadcast / auth总是因403错误而失败
作者:互联网
我最近钻研了Laravel 5.3的Laravel-Echo和Pusher组合.我已经成功建立了公共频道,然后转向私人频道.我在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路由就会失败.
解决方法:
错误403 /使用Laravel版本的broadcast / auth> 5.3& Pusher,您需要更改resources / 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
它对我有用,希望对你有所帮助.
标签:php,laravel,pusher,laravel-echo 来源: https://codeday.me/bug/20191006/1861469.html