๐Ÿ”‘

Devise

Rails standard authentication system

Devise is the most widely used authentication gem in Rails.

10 modules:

  • database_authenticatable โ€” password hashing and login

  • registerable โ€” sign up

  • recoverable โ€” password reset

  • rememberable โ€” auto login (Remember Me)

  • validatable โ€” email/password validation

  • confirmable โ€” email verification

  • lockable โ€” account lock on login failures

  • timeoutable โ€” session timeout

  • trackable โ€” login tracking

  • omniauthable โ€” OAuth (Google, GitHub, etc.)

Auto-generated helpers:

  • current_user โ€” currently logged-in user

  • user_signed_in? โ€” login status check

  • authenticate_user! โ€” redirect if not logged in

Customization is done through controller inheritance and view overrides.

Key Points

1

Add gem "devise" to Gemfile and bundle install

2

rails generate devise:install โ†’ generate initial config files

3

rails generate devise User โ†’ User model + migration generated

4

rails db:migrate โ†’ create devise tables

5

before_action :authenticate_user! โ†’ add to controllers requiring auth

6

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

Use Cases

Member-based web services Google/GitHub OAuth login Email verification flow Admin panel authentication