Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails devise hook into on_login

I'm looking to hook into devise after login / after session create. How would I go about doing this?

Basically I want to set a users location every time they login, and to do that I need an after login hook of sorts.

like image 801
Marc Avatar asked Nov 01 '11 18:11

Marc


2 Answers

Devise is built on Warden, so you can use Warden's after_authentication hook.

Put this in an initializer:

Warden::Manager.after_authentication do |user,auth,opts|
  # do something with user
end

The remote IP address and other request info is stored in auth.request (i.e. auth.request.remote_ip).

See https://github.com/hassox/warden/wiki/callbacks

like image 185
Nick Urban Avatar answered Nov 01 '22 00:11

Nick Urban


Devise updates the value of the user.current_sign_in_at timestamp on successful login. So, you could simply add a before_save filter to your User model. In that filter, check to see if the value of this field has changed, and if it has, set the users location.

BTW - I'm not sure what you mean by "location" - if you mean IP address, Devise already stores that for you.

like image 22
cailinanne Avatar answered Oct 31 '22 23:10

cailinanne