Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access Session Data #11

Open
philmccarty opened this issue Apr 21, 2014 · 4 comments
Open

Access Session Data #11

philmccarty opened this issue Apr 21, 2014 · 4 comments

Comments

@philmccarty
Copy link

Is there any way to access laravel's Session data from within BrainSocketEventListener.php?

Traditional calls, like Session::get($variable) give me an error indicating that BrainSocketEventListener has no idea what Session:: is.

@victorhqc
Copy link

I have a similar problem, the Auth::check() and Auth:user() doesn't seem to be working properly and the network information seems to be sending the correct cookies

@interprogs
Copy link

Hi,

I have the same problem , does somebody have a solution please ?

Thx

@epicwally
Copy link

I have come up with a workaround. It involves taking the laravel_session cookie, decrypting it, and then retrieving the session from the session_id. (Note: I'm using the file driver for sessions.)

Here's my code:

In the EventListener, under onMessage I added:

$session_name = $from->WebSocket->request->getCookies()['laravel_session'];
$session_id = Crypt::decrypt($session_name);
$session_array = @unserialize(Session::getHandler()->read($session_id));

You can then grab the user_id associated with the session by accessing the 'login_##########' key of this session_array object. You'll have to open up one of the session files to find what this key is, but it should be the same across all sessions.

Once you have the user_id, you can just do a User::find() on it, and voila, you have access to your user model.

To include the username with the response, you will need to json_decode the $msg variable, and then add a new attribute under $json->client->data. and then re-encode it. Ex.

$user = User::find($user_id);
$json = json_decode($msg);
$json->client->data->username = (string)$user->username;
$msg = json_encode($json);

Hope that helps someone! Took me a lot of beating my head against the wall to come up with this one.

@victorhqc
Copy link

Nice, Ill try it! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants