Skip to content
This repository was archived by the owner on Jan 15, 2023. It is now read-only.

Commit

Permalink
Add browser info parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jclusso committed Jun 28, 2022
1 parent c18b5a7 commit 0094243
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ A `LoginActivity` record is created every time a user tries to login. You can th
- `ip` - IP address
- `user_agent` and `referrer` - from browser
- `city`, `region`, `country`, `latitude`, and `longitude` - from IP
- `browser` and `platform` - from `user_agent` parsing
- `created_at` - time of event

## Features
Expand Down Expand Up @@ -188,6 +189,23 @@ end

Check out [this example](https://github.com/ankane/authtrail/issues/40)

## Browser Info

AuthTrail uses [Browser](https://github.com/fnando/browser) for parsing
information about the browser from the User-Agent.

To enable browser info, add this line to your application’s Gemfile:

```ruby
gem "browser"
```

And update `config/initializers/authtrail.rb`:

```ruby
AuthTrail.browser_info = true
```

## Data Retention

Delete older data with:
Expand Down
12 changes: 11 additions & 1 deletion lib/authtrail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

module AuthTrail
class << self
attr_accessor :exclude_method, :geocode, :track_method, :identity_method, :job_queue, :transform_method
attr_accessor :exclude_method, :geocode, :track_method, :identity_method,
:job_queue, :transform_method, :browser_info
end
self.geocode = false
self.browser_info = false
self.identity_method = lambda do |request, opts, user|
if user
user.try(:email)
Expand All @@ -33,6 +35,14 @@ def self.track(strategy:, scope:, identity:, success:, request:, user: nil, fail
referrer: request.referrer
}

if AuthTrail.browser_info
browser = Browser.new(request.user_agent)
data.merge!(
browser: browser.name,
platform: browser.platform.name
)
end

if request.params[:controller]
data[:context] = "#{request.params[:controller]}##{request.params[:action]}"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
t.string :country
t.float :latitude
t.float :longitude
t.string :browser
t.string :platform
t.datetime :created_at
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/internal/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
t.string :country
t.float :latitude
t.float :longitude
t.string :browser
t.string :platform
t.datetime :created_at

# app-specific fields
Expand Down

0 comments on commit 0094243

Please sign in to comment.