3

I'm currently in the process of removing IP logging from our app, I was wondering what's the best way to go about doing this with Devise?

Thermatix
  • 2,757
  • 21
  • 51

2 Answers2

3

Your answer looks good, but if you're looking to track the IP for only specific users, one (less verbose but perhaps more confusing) alternative, could be...

protected

# Override Devise logic for IP tracking
# https://github.com/plataformatec/devise/blob/master/lib/devise/models/trackable.rb#L45
def extract_ip_from(request)
  # Only track the IP for admin users (per GDPR rules).
  request.remote_ip if admin?
end

This would cause a nil IP to be set for non-admin users.

Ollie Bennett
  • 4,424
  • 1
  • 19
  • 26
2

Adding this method to the user model can allow you to be selective in what you track, in my case I'm being selective in when I track IP addresses:

  def update_tracked_fields(request)
    old_current = current_sign_in_at
    new_current = Time.now.utc
    self.last_sign_in_at     = old_current || new_current
    self.current_sign_in_at  = new_current

    if admin?
      old_current = current_sign_in_ip
      new_current = request.remote_ip
      self.last_sign_in_ip     = old_current || new_current
      self.current_sign_in_ip  = new_current
    end

    self.sign_in_count ||= 0
    self.sign_in_count += 1
  end
Thermatix
  • 2,757
  • 21
  • 51