What Is Devise? The Gem That Adds Login and Sign-Up to Rails
From current_user and authenticate_user! to password resets
Devise is the most widely used authentication gem in Rails.
10 modules:
database_authenticatable— password hashing and loginregisterable— sign uprecoverable— password resetrememberable— auto login (Remember Me)validatable— email/password validationconfirmable— email verificationlockable— account lock on login failurestimeoutable— session timeouttrackable— login trackingomniauthable— OAuth (Google, GitHub, etc.)
Auto-generated helpers:
current_user— currently logged-in useruser_signed_in?— login status checkauthenticate_user!— redirect if not logged in
Customization is done through controller inheritance and view overrides.
Key Points
Add gem "devise" to Gemfile and bundle install
rails generate devise:install → generate initial config files
rails generate devise User → User model + migration generated
rails db:migrate → create devise tables
before_action :authenticate_user! → add to controllers requiring auth
Use helpers like current_user, user_signed_in?
Pros
- ✓ Rich features (10 modules)
- ✓ Large community with rich documentation
- ✓ Security best practices applied (bcrypt, CSRF)
- ✓ Easy social login with OmniAuth
Cons
- ✗ Customization can be complex
- ✗ Too much magic makes internals hard to understand
- ✗ Excessive features for API-only apps
- ✗ Need generate devise:views for view customization