BACKTRACE

(condense)

Back to Posts List

GET

Find all online users with Authlogic

Assuming you want to find all the users that are currently online when you use Authlogic, you might find it’s a little tricky at first since you can’t really access the sessions table from UserSession.

Depending on your SessionStore configuration, you can theoretically access the storage directly (the Sessions table, or memcached for example), but assuming you don’t want to add some unnecessary code or models to your app, you can simply use one of Authlogic’s Magic columns.

Hard way

Authlogic gives you all kind of automatic columns that you don’t really need to update or maintain on your own, they are maintained by the actual code flow of Authlogic itself.
Those fields can contain some basic functionality related issues like the number of login attempts made, the ip address from which the attempt was made an or even what was the ip address the last time that user logged in. fun.

The magic column that will help us find who is probably online is the one called last_request_on, which basically indicates when was the last time that user made a request to your application.

The second parameter we’ll need in order to make a more accurate selection, is the configuration option named logged_in_timeout, which sets the timeout after which a stale session will be expired, by default it will expire after 10 minutes.

so if you set your session expiry to 30 minutes:

searching for those users is pretty easy:

(note that this one counts, you can do whatever you want)

Easy way

As Frederic mentioned in the first comment for this post, Authlogic gives us an easier way to access the logged in users by giving us a few scopes we can use such as logged_in and logged_on (Authlogic::ActsAsAuthentic::LoggedInStatus)

So, in order to get all the online users we can simply:

which is way easier, thanks Frederic.

POST

blog comments powered by Disqus

I Don't have cookies.

ELAD ENV

Variable Value
LINKEDIN
TWITTER
FACEBOOK
GITHUB
WWR
IRC
{ 'irc.freenode.net' => [ '#rubyonrails', '#railsbridge', '#ruby', '#mootools' ]}
SKYPE
eladmeidar

You're seeing this error because I think it is funny.