具有Laravel 5身份验证的Pusher [英] Pusher with Laravel 5 Authentication

查看:123
本文介绍了具有Laravel 5身份验证的Pusher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Laravel 5中使用Live Chat制作应用程序,并且正在关注本教程 https://github.com/dazzz1er/confer/tree/master 我已经关注了所有这些内容,但是我的网络控制台出现了错误:

I'm making an app with Live Chat in Laravel 5 and I'm following this tutorial, https://github.com/dazzz1er/confer/tree/master I already followed all of them but I'm having an error in my web console:

好像是在我的url上进行ajax调用 http://localhost/joene_/public /index.php/auth ,由于我没有处理该请求的路由,因此显示404.我不知道是否应该为此路由,但是我该在上面编写什么代码?我不知道.本教程甚至都没有提到它.

Seems like it's making an ajax call on my url http://localhost/joene_/public/index.php/auth and since I don't have a route to handle that request, it says 404. I don't know if should make a route for it but what will I code on there? I have no idea. The tutorial doesn't even mention it.

谢谢

推荐答案

只要您调用Auth::check(),Laravel就会通过检查其会话信息来验证用户是否已通过身份验证.

Whenever, you call Auth::check(), Laravel will verify if the user is authenticated by checking its session information.

按钮如何?他们怎么知道,哪些用户当前已在您的laravel应用程序上登录?

What about Pusher? How will they know, which users are currently logged in on your laravel application?

答案在于ajax调用http://localhost/joene_/public/index.php/auth.

The answer lies in the ajax call http://localhost/joene_/public/index.php/auth.

通过调用上述URL,您的laravel安装将使您的Pusher应用程序与用户的laravel会话链接.

By calling the above URL, your laravel installation will let your Pusher application link with your users' laravel session.

让我们深入研究一些代码:

1)推送器身份验证控制器

class PusherController extends Controller {

    //accessed through '/pusher/'
    //setup your routes.php accordingly

    public function __construct() {
        parent::__construct();
        //Let's register our pusher application with the server.
        //I have used my own config files. The config keys are self-explanatory.
        //You have received these config values from pusher itself, when you signed up for their service.
        $this->pusher = new Pusher(\Config::get('pusher.app_key'), \Config::get('pusher.app_secret'), \Config::get('pusher.app_id'));
    }

    /**
     * Authenticates logged-in user in the Pusher JS app
     * For presence channels
     */
    public function postAuth()
    {
        //We see if the user is logged in our laravel application.
        if(\Auth::check())
        {
            //Fetch User Object
            $user =  \Auth::user();
            //Presence Channel information. Usually contains personal user information.
            //See: https://pusher.com/docs/client_api_guide/client_presence_channels
            $presence_data = array('name' => $user->first_name." ".$user->last_name);
            //Registers users' presence channel.
            echo $this->pusher->presence_auth(Input::get('channel_name'), Input::get('socket_id'), $user->id, $presence_data);       
        }
        else
        {
            return Response::make('Forbidden',403);
        }
    }
}

2)与Pusher一起使用的JS

//replace 'xxx' below with your app key
var pusher = new Pusher('xxx',{authEndpoint : '/pusher/auth'});
var presenceChannelCurrent = pusher.subscribe('presence-myapp');
presenceChannelCurrent.bind('pusher:subscription_succeeded', function() {
    alert(presenceChannelCurrent.members.me.info.name+' has successfully subscribed to the Pusher Presence Channel - My App');
});

希望它对您有帮助.

这篇关于具有Laravel 5身份验证的Pusher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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