Search code examples
ruby-on-railsauthenticationdeviseip

Record IP Address of user when they log in with Devise (Rails 3.2)


I'm using Devise to handle my authentication for my site. Is there a way I can add a function to record a user's IP address when they log into my app?

We are just trying to see where people are logging in from.

Thanks

Edit

It's been pointed out that the devise user model watches the current and last_ip of the logged in user. I want to keep a record of every login.


Solution

  • You can use the Warden hooks inside of User.rb to do whatever you want.

    Warden::Manager.after_set_user do|record, warden, opts|
      logger.info("sign in at: #{record.current_sign_in_at}, #{record.current_sign_in_ip}")
      record.account_logins.create!(ipAddress: record.current_sign_in_ip)
    end