Tyler DeWitt
Tyler DeWitt

Reputation: 23576

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.

Upvotes: 3

Views: 8164

Answers (3)

tiny_violin
tiny_violin

Reputation: 11

You can also use request.ip or request.remote_ip to see an IP address. See link

Upvotes: 0

Aj Gu
Aj Gu

Reputation: 1439

devise stores the current and last ip address of the user automatically in user table. The column names it uses are current_sign_in_ip, last_sign_in_ip. To track all the sessions of a user you could use https://github.com/shenoudab/devise_traceable

Upvotes: 3

Tyler DeWitt
Tyler DeWitt

Reputation: 23576

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

Upvotes: 2

Related Questions