Track Devise login activity
🍊 Battle-tested at Instacart
Add this line to your application’s Gemfile:
gem 'authtrail'And run:
rails generate authtrail:install
rake db:migrateA LoginActivity record is created every time a user tries to login. You can then use this information to detect suspicious behavior. Data includes:
scope- Devise scopestrategy-database_authenticatablefor password logins,rememberablefor remember me cookie, or the name of the OmniAuth strategyidentity- email addresssuccess- whether the login succeededfailure_reason- if the login faileduser- the user if the login succeededcontext- controller and actionip- IP addressuser_agentandreferrer- from browsercity,region, andcountry- from IPcreated_at- time of event
IP geocoding is performed in a background job so it doesn’t slow down web requests. You can disable it entirely with:
AuthTrail.geocode = falseExclude certain attempts from tracking - useful if you run acceptance tests
AuthTrail.exclude_method = proc do |info|
info[:identity] == "[email protected]"
endWrite data somewhere other than the login_activities table.
AuthTrail.track_method = proc do |info|
# code
endSet job queue for geocoding
AuthTrail::GeocodeJob.queue_as :lowWe recommend using this in addition to Devise’s Lockable module and Rack::Attack.
Works with Rails 5+
View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features