๐ก๏ธ Rails::Auth
Rails::Auth is a high-performance, security-first authentication engine for Ruby on Rails. Designed as a modern, transparent alternative to Devise, it empowers users with deep visibility and control over their account security through database-backed sessions and enterprise-grade protection.
๐ Table of Contents
- ๐ Key Features
- ๐ฆ Installation
- ๐ ๏ธ Getting Started
- ๐ Usage
- ๐ก๏ธ Security Dashboard
- โ๏ธ Configuration
- ๐จ Customization
- ๐ฅ Authors & Maintainers
- ๐ License
๐ Key Features
๐ Enterprise Security
- JWT Authentication: Built-in support for stateless API authentication via
Authorization: Bearer <token>. - Security Audit Logs: Comprehensive tracking of sensitive actions (login failures, MFA toggles, password changes).
- Admin Impersonation: Securely log in as any user for customer support while tracking the original admin's actions.
- Two-Factor Authentication (MFA): Modern TOTP support (Google Authenticator, Authy) with automated QR codes.
- Account Locking (Lockable): Protection against brute-force attacks with email-based unlocking.
- Email Confirmation (Confirmable): Ensure every user is verified before they can access your app.
- Role-Based Access Control (RBAC): Built-in
user,moderator, andadminroles with clean authorization helpers.
๐ฑ Advanced Session Management
- Database-Backed Sessions: Every device login is tracked, allowing for complete transparency.
- Remote Device Revocation: Log out of lost or stolen devices remotely from the dashboard.
- Global Sign-Out: Instant "Sign out of all devices" for emergency account security.
- Device Intelligence: Automatically identifies Browser, OS, and IP Address for every session.
๐ ๏ธ Developer Experience
- Active Storage Ready: Seamlessly integrated avatar/profile picture support.
- Model Independent: Easily attach to
User,Admin,Member, or any other model. - Minimalistic Core: Built on top of Rails' native
has_secure_password. - Generator Driven: Scaffold everything you need in seconds.
๐ฆ Installation
Add this line to your application's Gemfile:
gem "rails-auth"
Then execute:
$ bundle install
๐ ๏ธ Getting Started
1. Run the Installer
Scaffold the configuration and engine routes:
$ rails g rails_auth:install
2. Generate Your Authentication Model
Create or update your user model (e.g., User):
$ rails g rails_auth:model User
This will generate:
- The
User&Sessionmodels. - Migrations for MFA, Lockable, Confirmable, and RBAC.
3. Run Migrations
$ rails db:migrate
๐ Usage
Base Controller Setup
Include the authentication concern in your ApplicationController:
class ApplicationController < ActionController::Base
include Rails::Auth::AuthenticatableController
end
Controller Helpers
| Helper | Description |
|---|---|
authenticate_user! |
Before action to ensure the user is logged in. |
current_user |
Returns the currently authenticated user. |
current_session |
Returns the database record for the current session. |
user_signed_in? |
Check if a user is currently authenticated. |
require_admin! |
Authorization helper to restrict access to admins. |
authorize_role!(*roles) |
Restrict access to specific roles (e.g., :moderator, :admin). |
Role-Based Access Control (RBAC)
class Admin::SettingsController < ApplicationController
before_action :require_admin!
def update
# Only admins can reach here
end
end
Avatar Support
Rails::Auth uses Active Storage for avatars. Once Active Storage is installed in your host app:
<% if current_user.avatar.attached? %>
<%= image_tag current_user.avatar.variant(resize_to_limit: [50, 50]) %>
<% end %>
JWT API Authentication
Rails::Auth automatically supports JWTs for API endpoints.
- Send a
POSTto/auth/session.jsonwith email/password. - Receive a token:
{ "token": "eyJhb..." }. - Use it in subsequent requests:
Authorization: Bearer eyJhb...
Admin Impersonation
Admins can impersonate users for support purposes:
# Start Impersonation (Controller action)
sign_in(user, impersonated_by: current_user)
# Stop Impersonation
rails_auth.stop_impersonations_path, method: :delete
Note: Impersonation events are securely logged in the SecurityEvent table.
๐ก๏ธ Security Dashboard
Users can manage their security settings, view active sessions, and enable MFA at /auth/security/sessions.
Linking to the Dashboard
<%= link_to "Security Settings", rails_auth.security_sessions_path %>
โ๏ธ Configuration
Customize the gem behavior in config/initializers/rails_auth.rb:
Rails::Auth.setup do |config|
config.user_class_name = "User"
config.session_class_name = "Session"
end
๐จ Customization
Views
Override the default views by copying them to your application:
$ rails g rails_auth:views
Controllers
You can inherit from Rails::Auth::ApplicationController or include the AuthenticatableController in your own controllers for deep integration.
๐งช Testing
Rails::Auth is tested against modern Rails versions. Run the suite:
$ bundle exec rake test
๐ฅ Authors & Maintainers
- Shiboshree Roy - Lead Developer
๐ License
The gem is available as open source under the terms of the MIT License.
Built with โค๏ธ for the Rails Community by Shiboshree Roy.