Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel, get currently logged-in users

I want to display a list of currently logged-in users in an app. I want to use Laravel Auth method. I'm looking at the API and I cannot find anything like it.

I would probably need to loop through the sessions store and then match it to a user ID. Am I right?

UPDATE: Forgot to mention, I'm storing sessions in the DB.

like image 613
Mission Avatar asked Mar 26 '13 08:03

Mission


1 Answers

"Currently logged in" is something you can't do with plain old sessions. Let me explain why:

A session is a bunch of data stored at server side which is assigned to an user through a cookie. That cookie remains on user browser and so it keeps the session active. Sessions can stay "alive" months without the user even logging in.

But, it's possible to store sessions on database.

As you can see, Laravel keeps a field called last_activity and, through that field, you should be able to retrieve all sessions that had activity within the last 15 minutes (or something else, you call it).

When your retrieve those records, the data field is a serialized representation of session data. You can unserialize($session_record->data) and retrieve the user id.

Depending on your Auth driver, session's user id may have different names:

  • For eloquent driver, it should be eloquent_login.
  • For fluent driver fluent_login.
  • For your Custom\AuthClass, it should be called custom_authclass_login.
like image 181
vFragosop Avatar answered Oct 20 '22 19:10

vFragosop