Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

authtrail

> 编程语言
Open source

Track Devise login activity

566 stars0 likes0 views
WebsiteGitHub

About

Track Devise login activity

AuthTrail

Track Devise login activity

:tangerine: Battle-tested at Instacart

Installation

Add this line to your application’s Gemfile:

ruby
gem "authtrail"

To encrypt email and IP addresses with Lockbox, install Lockbox and Blind Index and run:

bash
rails generate authtrail:install --encryption=lockbox
rails db:migrate

To use Active Record encryption, run:

bash
rails generate authtrail:install --encryption=activerecord
rails db:migrate

If you prefer not to encrypt data, run:

bash
rails generate authtrail:install --encryption=none
rails db:migrate

To enable geocoding, see the Geocoding section.

How It Works

A 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 scope
  • strategy - Devise strategy
  • identity - email address
  • success - whether the login succeeded
  • failure_reason - if the login failed
  • user - the user if the login succeeded
  • context - controller and action
  • ip - IP address
  • user_agent and referrer - from browser
  • city, region, country, latitude, and longitude - from IP
  • created_at - time of event

Features

Exclude certain attempts from tracking - useful if you run acceptance tests

ruby
AuthTrail.exclude_method = lambda do |data|
  data[:identity] == "[email protected]"
end

Add or modify data - also add new fields to the login_activities table if needed

ruby
AuthTrail.transform_method = lambda do |data, request|
  data[:request_id] = request.request_id
end

Store the user on failed attempts

ruby
AuthTrail.transform_method = lambda do |data, request|
  data[:user] ||= User.find_by(email: data[:identity])
end

Write data somewhere other than the login_activities table

ruby
AuthTrail.track_method = lambda do |data|
  # code
end

Use a custom identity method

ruby
AuthTrail.identity_method = lambda do |request, opts, user|
  if user
    user.email
  else
    request.params.dig(opts[:scope], :email)
  end
end

Associate login activity with your user model

ruby
class User "]
  data[:region] = request.headers[""]
  data[:city] = request.headers[""]
end

Check out this example

Data Retention

Delete older data with:

ruby
LoginActivity.where("created_at < ?", 2.years.ago).in_batches.delete_all

Delete data for a specific user with:

ruby
LoginActivity.where(user_id: 1, user_type: "User").in_batches.delete_all

Other Notes

We recommend using this in addition to Devise’s Lockable module and Rack::Attack.

Check out Hardening Devise and Secure Rails for more best practices.

History

View the changelog

Contributing

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

To get started with development and testing:

bash
git clone https://github.com/ankane/authtrail.git
cd authtrail
bundle install
bundle exec rake test

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言