Pusher with Laravel 5 Authentication

不想你离开。 提交于 2020-01-13 11:23:16

问题


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:

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.

Thanks


回答1:


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

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

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

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

Let's dive into some code:

1) Pusher Auth Controller

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) JS used with Pusher

//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');
});

Hope it helps you.



来源:https://stackoverflow.com/questions/30395219/pusher-with-laravel-5-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!