Warden Callbacks - multiple hits

123 views
Skip to first unread message

paulo

unread,
Dec 21, 2010, 6:34:23 AM12/21/10
to Devise
Hi all,

I'm trying to get the Warden:Callbacks to work on a an app, so that
all user logins are recorded in a 'user_login' table, recording the
user_id, ip address and if they have logged in or out (1 or 0).

The code I have so far, in the user model is:

Warden::Manager.after_authentication do |user,auth,opts|

store=UserLogin.new(:user_id=>user.id, :ip_address=>"123.456.789.0", :login_type=>1)
store.save
end
Warden::Manager.before_logout do |user,auth,opts|

store=UserLogin.new(:user_id=>user.id, :ip_address=>"123.456.789.0", :login_type=>0)
store.save
end


This records the information as required, but multiple rows (varies
from 2 to 11 rows per login / logout).

Am I correct in having these callbacks in the user model? I suspect
this may be where I am going wrong with it?


Help much appreciated as always,

Paul

Carlos Antonio da Silva

unread,
Dec 21, 2010, 6:36:29 AM12/21/10
to plataforma...@googlegroups.com
These callbacks should be initialized once and just once, so a better place to put them is config/initializers.
--
At.
Carlos A. da Silva

paulo

unread,
Dec 21, 2010, 7:04:52 AM12/21/10
to Devise
Thanks for that Carlos,

I suspected that was going to be the way to do it but it seems to
complicate matters when getting the IP address



On Dec 21, 11:36 am, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
Dec 21, 2010, 7:23:14 AM12/21/10
to plataforma...@googlegroups.com
You'd get the ip address from the request, that should be available through the warden object.

    auth.request.remote_ip (or sth like that)

Take a look at warden docs for more info.

paulo

unread,
Dec 21, 2010, 11:15:19 AM12/21/10
to Devise
Again, that worked a treat.

Thanks Carols

On Dec 21, 12:23 pm, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
Dec 21, 2010, 11:18:13 AM12/21/10
to plataforma...@googlegroups.com
No problem =)
Reply all
Reply to author
Forward
0 new messages