Thermatix
Thermatix

Reputation: 2929

Disable IP address logging with devise

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?

Upvotes: 3

Views: 715

Answers (2)

Thermatix
Thermatix

Reputation: 2929

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

Upvotes: 2

Ollie Bennett
Ollie Bennett

Reputation: 4484

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.

Upvotes: 3

Related Questions