Rails and Authlogic. Show currently registered users

I would like to have a list of registered users.

This code does not work:

<% UserSession.all.each do |user_session| %> <% end %> 
+4
source share
5 answers

@ syed-aslam has a good solution, but you can just let Authlogic do the job. Check out the Authlogic :: ActsAsAuthentic :: LoggedInStatus module , which defines two areas: logged_in , logged_out

Your code will look like this:

 <% User.logged_in.each do |user| %> <% end %> 

PS I usually refer to RDoc instead of the source code, but RDoc seems to have problems at the moment.

+5
source

Authlogic provides you with all kinds of automatic columns that you really don't need to update or maintain yourself; they are supported by the actual code flow of Authlogic itself. These fields may contain some basic functionality-related problems, such as the number of login attempts, the IP address from which the attempt was made, or even what was the IP address the last time the user logged in. Fun

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

For a more accurate choice, a second parameter is needed - this is a configuration parameter named logged_in_timeout, which sets a timeout after which an outdated session expires, by default it expires after 10 minutes.

therefore, if you set the session expiration to 30 minutes:

 class User << ActiveRecord::Base acts_as_authentic do |c| c.logged_in_timeout 30.minutes end end 

searching for these users is quite simple:

 module OnlineUsers def count_online_users User.count(:conditions => ["last_request_at > ?", 30.minutes.ago]) end end 
+4
source

You cannot get a UserSession for all users, a UserSession is created every time a user sends a request and is not remembered between requests.

However, you can show users who are logged in at a specific time period (if you have the last_logged_in column updated every time you sign)

 Logged in last 15 minutes: <% User.find("last_logged_in < ?", 15.minutes.ago ).each do |user| %> 
0
source

Why not create a field called currently_active in the user model and update it to true after creating the session and update it to false after destroying the session.

You can then call User.where(currently_active: true) to provide users who are on the Internet.

0
source

I wrote after_create and before_destroy callbacks in the UserSession model. In the after_create callback, I wrote the user ID to enter the text file (self.user.id) and in the before_create callback I deleted the same. To check user activity, I read a text file and checked for a user ID in this file.

-1
source

Source: https://habr.com/ru/post/1340295/


All Articles