skip to content

With Devise 5, which behaviour does each of the ten model modules supply, and which five does `rails generate devise User` enable?

level: middleimportance: must knowfreq 62%

answer

  1. two modules are Warden strategies
  2. database_authenticatable owns encrypted_password
  3. registerable, recoverable, rememberable, validatable
  4. confirmable and lockable add columns and controllers
  5. timeoutable and trackable work through Warden hooks

basics

~10 s

The generator enables database_authenticatable, registerable, recoverable, rememberable and validatable. Confirmable, lockable, timeoutable, trackable and omniauthable are opt-in; each module adds its own columns, routes, controller or Warden hook to the model.

solid answer

~40 s

`rails generate devise User` writes `devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable` and leaves the other five commented. `database_authenticatable` hashes the password into `encrypted_password` and signs in from a form; `rememberable` keeps a signed remember-me cookie; `registerable` gives sign-up and account editing; `recoverable` emails a reset link valid for `reset_password_within` (6 hours); `validatable` adds email and password validations. Opt-in: `confirmable` blocks sign-in until the email link is clicked, `lockable` locks after `maximum_attempts` failures (20 by default), `timeoutable` ends idle sessions after `timeout_in` (30 minutes), `trackable` records sign-in counts, times and IPs, and `omniauthable` handles OmniAuth callbacks. Most add columns; registerable, validatable and timeoutable add none.

code

ruby · 8 lines
ruby
class User < ApplicationRecord
  # Default five from `rails generate devise User`
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         # opt-in modules, each with its own columns or hooks
         :confirmable, :lockable, :timeoutable, :trackable,
         timeout_in: 15.minutes
end

go deeper

for a junior

Name the five default modules and say in one line what confirmable, lockable and recoverable add.

for a middle

Map each module to its columns, controller and config key, and explain active_for_authentication? as the shared gate.

for a senior

Reason about module interplay in production: remember-me versus timeout, lockable counters, confirmable grace periods and per-model overrides.

for a principal

Decide which modules a product really needs and what each one commits you to in schema, mail and support load.

## How modules plug into a model **Devise** ships ten **modules**, each a Ruby module mixed into the model by the `devise` macro. A module can contribute up to four things: - **model methods and callbacks** (for example `confirm`, `lock_access!`, `valid_password?`); - **database columns** it expects, listed by the module's `required_fields`; - a **controller and routes**, drawn by `devise_for` only when the model enables the module; - a **Warden hook** or **strategy** that runs on sign-in or on every request. Two of the ten, `database_authenticatable` and `rememberable`, are registered as **Warden strategies**: they are the ways a user actually gets authenticated. The rest decorate that process. Devise sorts the modules into a fixed internal order whatever order you list them in, so `devise :validatable, :database_authenticatable` behaves the same as the reverse. ## The ten modules | Module | Adds | Columns | Controller | |---|---|---|---| | `database_authenticatable` | bcrypt-hashed password, `valid_password?`, form sign-in strategy | `email`, `encrypted_password` | sessions | | `rememberable` | signed `remember_user_token` cookie, `remember_for` 2 weeks | `remember_created_at` | none | | `registerable` | sign up, edit account, cancel account | none | registrations | | `recoverable` | emailed reset link, `reset_password_within` 6 hours | `reset_password_token`, `reset_password_sent_at` | passwords | | `validatable` | email presence, uniqueness and format; password presence, confirmation, length `6..128` | none | none | | `confirmable` | confirmation email; sign-in refused until confirmed | `confirmation_token`, `confirmed_at`, `confirmation_sent_at`, `unconfirmed_email` | confirmations | | `lockable` | lock after `maximum_attempts` (20); unlock by email, time or both | `failed_attempts`, `unlock_token`, `locked_at` | unlocks | | `timeoutable` | sign out after `timeout_in` (30 minutes) idle | none (session key) | none | | `trackable` | `sign_in_count`, current and last sign-in time and IP | five columns | none | | `omniauthable` | OmniAuth callback routes and helpers | usually `provider`, `uid` (yours) | omniauth_callbacks | `rails generate devise User` enables the first five rows. Its migration writes the columns for those and leaves the trackable, confirmable and lockable columns commented out, ready to uncomment. ## What the generators write Setting Devise up is two generator runs: 1. `rails generate devise:install` writes `config/initializers/devise.rb`, where every module's options live, and `config/locales/devise.en.yml` with the flash and failure messages. 2. `rails generate devise User` writes the model's `devise` line with the five defaults and a comment naming the other five, a migration, and the route `devise_for :users`. Because `devise_for` draws routes per enabled module, adding `:confirmable` later adds the `/users/confirmation` routes without touching `config/routes.rb`. The default views ship inside the gem; `rails generate devise:views` copies them into the app only when you want to change them. ## How the opt-in modules hook in The modules cooperate through a few shared methods rather than calling each other: 1. **`active_for_authentication?`** decides whether an authenticated record may stay signed in. `confirmable` makes it false for unconfirmed users (after the `allow_unconfirmed_access_for` grace, which defaults to `0.days`), and `lockable` makes it false while a lock is in force (`locked_at` set and, under a time strategy, not yet expired). A Warden `after_set_user` hook checks it every time the user is loaded, sign-in or session fetch, and signs the user out with the `inactive_message` (`:unconfirmed`, `:locked`). 2. **`valid_for_authentication?`** wraps the password check. `lockable` uses it to increment `failed_attempts` and lock the account when the limit is reached. 3. **Warden hooks** do per-request work: `timeoutable` keeps `last_request_at` in the scope's session and signs the user out when it is older than `timeout_in`; `trackable` updates its columns when a user is explicitly signed in, not on every request. ## Configuration: global and per model Every option has a global default in `config/initializers/devise.rb` (`config.maximum_attempts`, `config.timeout_in`, `config.remember_for`...). The same keys can be passed to the macro for one model, for example `devise :database_authenticatable, :lockable, maximum_attempts: 5`, which overrides the global value for that class only. ## Common misreadings - `validatable` is optional; apps that need different rules drop it and write their own validations. - `trackable` is not an audit log: it keeps only the current and previous sign-in. - `omniauthable` does not create accounts by itself; the callback controller you write does (owned by the OmniAuth topic). - `timeoutable` and `rememberable` interact: a valid remember-me cookie keeps a session alive past the timeout.

  • Which Devise modules are Warden strategies, and why does the distinction matter?
    `database_authenticatable` and `rememberable`. They are the ways a request becomes authenticated: the sign-in form and the remember-me cookie. The other modules only decorate that process, through `active_for_authentication?`, `valid_for_authentication?` or Warden hooks, so removing one of them never stops a user signing in, while removing both leaves no built-in sign-in path except code that calls `sign_in` itself, as an OmniAuth callback does.
  • A user with a valid remember-me cookie never seems to time out under timeoutable; is that a bug?
    No. The timeoutable hook skips the sign-out while the remember-me cookie is still valid (`remember_me_is_active?`). If idle sessions must end regardless, do not offer remember-me for that scope, or shorten `remember_for`.

  • validatable is required for Devise to work at all
  • trackable updates its columns on every request
  • The order of modules in the devise call changes their behaviour
  • registerable adds its own columns to the users table
  • rails generate devise User enables confirmable by default