ArrowChat integration to Laravel 5.3+

Recently I’ve bought Arrowchat (https://www.arrowchat.com) for one of my customers. During the installation of arrowchat, you have to set up some users table values like:

  • Table name
  • user id field name
  • user name field name

This works correctly, but Arrowchat can not read the logged in user id, because usually Laravel encrypts the cookies, just like session cookie.

To solve this, we should bootstrap Laravel in the integration file of Arrowchat.

So the start of the integration.php should be start like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$base_path = dirname(dirname(dirname(dirname(__FILE__))));
require_once($base_path . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
require_once($base_path . DIRECTORY_SEPARATOR . 'bootstrap' . DIRECTORY_SEPARATOR . 'autoload.php');
$app = require_once($base_path . DIRECTORY_SEPARATOR . 'bootstrap' . DIRECTORY_SEPARATOR . 'app.php');
 
try {
    $app->make('Illuminate\Contracts\Http\Kernel')->handle(Illuminate\Http\Request::capture());
 
    $id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]);
 
     $app['session']->driver()->setId($id);
     $app['session']->driver()->start();
 
} catch(\Exception $ex) {
}

 

This bootstraps the Laravel app. But to receive the correct user id from Laravel we should rewrite the get_user_id function to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function get_user_id() {
    $userid = NULL;
    global $app;
 
    $app->make('Illuminate\Contracts\Http\Kernel')->handle(Illuminate\Http\Request::capture());
 
    $id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]);
    $app['session']->driver()->setId($id);
    $app['session']->driver()->start();
 
    if($app['auth']->user()!= NULL){
         $userid = $app['auth']->user()->id;
    }
 
    return $userid;
}

And that’s it! Arrowchat now can read the user id from the session, and can display logged in users correctly!

Vélemény, hozzászólás?

Az email címet nem tesszük közzé. A kötelező mezőket * karakterrel jelöljük.