skip to content

Rails 8 now generates its own authentication — for a stock-room tool that keeps growing, when would you keep the generated code and when adopt Devise?

level: principalimportance: should knowfreq 35%

answer

  1. the output is code you own
  2. template fixes never reach old apps
  3. count the missing features
  4. password_digest versus encrypted_password
  5. both hash with bcrypt

basics

~20 s

Keep the generated code while needs stay close to sign-in, sign-out and reset, because it is small and visible. Adopt Devise when registration, confirmation, lockout, timeouts or several user types pile up and you would otherwise maintain them all.

solid answer

~50 s

The generator writes a couple of hundred lines of application code, with no DSL and no gem beyond bcrypt, so every behaviour is readable and changeable. The cost is ownership: fixes to the template never reach an existing app, as Rails 8.1's added rate limit on password-reset requests shows, and everything it omits (sign-up, email confirmation, per-account lockout, session timeouts, social sign-in) is yours to build and secure. Devise supplies those as maintained modules (`:registerable`, `:confirmable`, `:lockable`, `:timeoutable`, `:omniauthable`) on Warden, at the price of a larger surface, overrides through its own controllers and views, and major-version upgrades. I keep the generated code when the feature list is short and the team will read it; I adopt the gem when the list is long or several user types need it. Moving later is feasible because both store bcrypt hashes, in `password_digest` and `encrypted_password` respectively.

go deeper

for a junior

Know that the generator writes plain code into your app with sign-in and reset but no sign-up, while Devise is a gem with optional modules.

for a middle

Name the features the generated code lacks and which Devise modules cover them, and explain that generated code never updates with Rails.

for a senior

Audit a generated app against the current template, estimate the cost of each missing feature, and plan a password-preserving move between the two.

for a principal

Decide by the feature roadmap, the team's willingness to own security code, and the number of user types, and say what would make you change course.

## Why the question exists Before Rails 8.0, an app needing passwords either hand-rolled them on `has_secure_password` or added Devise. Rails 8.0's `bin/rails generate authentication` changed the default answer for new apps, and interviewers now ask whether it removes the case for a gem. There is no single right answer; there are criteria. ## What the generator gives you - **Visible code.** Models, two controllers, a concern and a mailer, written into `app/`. A new developer can read the whole flow in minutes. - **Few dependencies.** Only `bcrypt`, for `has_secure_password`. - **Server-side sessions.** One `Session` row per browser, so revoking a session is a `DELETE`. - **Modern defaults.** `User.authenticate_by` with its timing defence, `rate_limit` on sign-in, stateless reset tokens that die when the password changes, `normalizes` on the email. ## What it costs you 1. **You own the code.** The generator runs once. When Rails improves the template, apps generated earlier do not change; Rails 8.1 added a rate limit to `PasswordsController#create`, and an app generated on 8.0 only gets it if someone adds it. 2. **You build the missing features.** Sign-up, email confirmation and re-confirmation, per-account lockout, idle and absolute session timeouts, "remember me" as a choice, social sign-in, multiple user types. 3. **You secure what you build.** Each added feature is new authentication code that no upstream project reviews or patches for you. ## What Devise gives and costs Devise 5 supplies the same ground as **modules** declared on the model (`:database_authenticatable`, `:registerable`, `:recoverable`, `:rememberable`, `:validatable`, `:confirmable`, `:lockable`, `:timeoutable`, `:trackable`, `:omniauthable`), on top of Warden. Its benefits are maintained features and a large body of shared knowledge; its costs are a bigger surface, customisation through generated controllers and views, and upgrades across majors (Devise 5 removed several long-standing helpers). The details of each module belong to Devise itself. | Criterion | Generated code | Devise | |---|---|---| | Features out of the box | sign-in, sign-out, reset | ten modules, opt-in | | Where the logic lives | your `app/` | the gem, overridden in your `app/` | | Security fixes | your responsibility | released upstream | | Customising a flow | edit the method | override a controller or hook | | Several user models | write it yourself | one `devise_for` per scope | | Session model | database rows, revocable | Warden data in Rails' session | ## A way to decide 1. **List the features the next year needs.** If the list is sign-in, reset and an admin-created account, as in an internal stock-room tool, the generated code fits and stays small. 2. **Count the ones Devise has and the generator lacks.** Two or three small ones (a sign-up form, a deactivation flag) are cheap to write. Confirmation, lockout, timeouts and social sign-in together are a project of their own. 3. **Ask who maintains it.** Generated code needs someone who reads it and follows Rails' template changes. A team that will not do that is safer on a maintained gem. 4. **Check for several user types.** Clerks and suppliers signing in separately is where Devise's scopes save real work. 5. **Keep a middle path in mind.** Generated authentication plus one focused gem (for example OmniAuth for social sign-in) often covers a growing app without adopting Devise wholesale. ## Moving later Switching is a migration, not a rewrite of users' passwords. Both store **bcrypt** hashes: the generator in `password_digest`, Devise in `encrypted_password`. Devise's comparison runs bcrypt on the stored hash and appends a pepper only when one is configured, so a copied `password_digest` verifies as long as no pepper is set. Sessions do not carry over, so everyone signs in once more.

  • Which generated pieces would you check first when auditing an app generated on Rails 8.0?
    Compare it with the 8.1 template: `PasswordsController` gained `rate_limit to: 10, within: 3.minutes, only: :create`, and tests gained `SessionTestHelper` with `sign_in_as` and `sign_out`. Then check the gaps the app has filled since, such as sign-up and deactivation, because that code is unreviewed by anyone outside the team.
  • Can existing users keep their passwords when moving from the generated code to Devise?
    Yes, if no Devise pepper is configured. Both store bcrypt hashes, and Devise's comparison hashes the submitted password with the stored hash's salt, so copying `password_digest` into `encrypted_password` keeps passwords working. Sessions do not carry over, so everyone signs in again.

  • Upgrading Rails keeps the generated authentication code up to date.
  • The generator is a thin wrapper that installs Devise with fewer modules.
  • Moving to Devise forces every user to reset their password.
  • The generated code includes confirmable and lockable behaviour like Devise.
  • Generated authentication is fine for any app because it uses has_secure_password.

Assumes Rails 8.1 and Devise 5.0; the authentication generator exists since Rails 8.0, and 8.1 changed its output.