首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 419在/broadcasting/auth出现页面过期错误,但csrfToken已添加LARAVEL通知

Laravel 419在/broadcasting/auth出现页面过期错误,但csrfToken已添加LARAVEL通知
EN

Stack Overflow用户
提问于 2020-11-07 18:12:44
回答 1查看 128关注 0票数 0

Laravel 7

我正在尝试在我的项目中添加Laravel Notification,但仍然停留在侦听通知上,主要是在广播/身份验证时收到错误。我已经解决了这个问题一次,但仍然没有收到通知。这是我的代码

应用\通知\收到的消息

代码语言:javascript
复制
class MessageReceived extends Notification
{

    use Queueable;



    /**
     * Create a new notification instance.
 *
 * @return void
 *
 */

public $notification;

public function __construct($notify)
{
    //
    $this->notification=$notify;
}

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['database'];
}

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
                ->line('The introduction to the notification.')
                ->action('Notification Action', url('/'))
                ->line('Thank you for using our application!');
}

/**
 * Get the array representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */


public  function toDatabase($notifiable){

    return [
        'MessageSender'=>$this->notification,
    ];
}



public function toArray($notifiable)
{
    return [
        'MessageSender'=>$this->notification,
    ];
}

public function toBroadcast($notifiable)
{
    return new BroadcastMessage([
        'MessageSender'=>$this->notification,
    ]);
}

我的守卫。管理模型

代码语言:javascript
复制
    class Admin extends Authenticatable
    {
        use Notifiable;
    
        protected $guard = 'admin';
        protected $fillable = [
            'name', 'email', 'password',
        ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function receivesBroadcastNotificationsOn()
    {

        return 'admin.'.$this->id;
    }
}

route/channel.php

代码语言:javascript
复制
Broadcast::channel('App.User.{id}', function ($user, $id) {

    return (int) $user->id === (int) $id;
});

Broadcast::channel('App.Admin.{id}', function ($admin, $id) {

    return (int) $admin->id === (int) $id;
},['guards' => ['admin']]);

public/ js /listner.js (我在js/app.js文件的链接下面使用了这个js文件链接,并测试了它的工作情况)

代码语言:javascript
复制
Echo.private('App.Admin.${id}' )
    .notification((notification) =>{
        window.alert(notification.type)
        console.log(notification)
    });

触发通知的控制器方法

代码语言:javascript
复制
 public function store(Request $request)
    {
        //

        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required',
            'message' => 'required'
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 422);
        }
        
        $admin = Admin::find(1);
       $admin->notify(new MessageReceived($request->email));


        return response()->json([
            'success'=>'Message sent successfully. we will get to you soon as possible',
//            'data'=>$noty,
        ], 200);
    }

resource/js/boostrap.js

代码语言:javascript
复制
 import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    forceTLS: true,
    authEndpoint: "/broadcasting/auth",
    auth: {
        headers: {
            'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
                },
                },
    // csrfToken:document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
});
window.Pusher.log = function(message ,err){
  console.log(message);
  console.log(err)
}

这是我所有的代码,但我仍然不能监听我触发的通知,我的环境文件中所有的值都是正确的,如BROADCAST_DRIVER=pusher和pusher键等

我需要一些关于这方面的指导,我哪里错了,我应该怎么做才能让这件事起作用

EN

回答 1

Stack Overflow用户

发布于 2020-11-09 14:23:24

Notification类中的via方法应该返回['broadcast']

还要从通知类中删除use Queueable;特征。

不要忘记在Pusher初始化器中添加namespace属性作为App.Events

代码语言:javascript
复制
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    forceTLS: true,
    authEndpoint: "/broadcasting/auth",
    namespace: 'App.Events'
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64726579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档