Flexible authentication solution for Rails with Warden
Switch branches/tags
Nothing to show
Pull request Compare This branch is 3245 commits behind plataformatec:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Devise is a flexible authentication solution for Rails based on Warden. It:

  • Is Rack based;

  • Is a complete MVC solution based on Rails engines;

  • Allows you to have multiple roles (or models/scopes) signed in at the same time;

  • Is based on a modularity concept: use just what you really need.

Right now it's composed of five mainly modules:

  • Authenticable: responsible for encrypting password and validating authenticity of a user while signing in.

  • Confirmable: responsible for verifying whether an account is already confirmed to sign in, and to send emails with confirmation instructions.

  • Recoverable: takes care of reseting the user password and send reset instructions.

  • Rememberable: manages generating and clearing token for remember the user from a saved cookie.

  • Validatable: creates all needed validations for email and password. It's totally optional, so you're able to to customize validations by yourself.

There's an example application using Devise at github.com/plataformatec/devise_example .


Devise is based on Warden (github.com/hassox/warden), a Rack Authentication Framework so you need to install it as a gem. Please ensure you have it installed in order to use devise (see instalation below).


All gems are on gemcutter, so you need to add gemcutter to your sources if you haven't yet:

sudo gem sources -a http://gemcutter.org/

Install warden gem if you don't have it installed (requires 0.5.0 or higher):

sudo gem install warden

Install devise inside your app:

sudo gem install devise

And you're ready to go.

Basic Usage

Devise must be setted up within the model (or models) you want to use, and devise routes must be created inside your routes.rb file.

We're assuming here you want a User model. First of all you have to setup a migration with the following fields:

create_table :users do

You may also want to add some indexes to improve performance:

add_index :your_table, :email
add_index :your_table, :confirmation_token    # for confirmable
add_index :your_table, :reset_password_token  # for recoverable

Now let's setup a User model adding the devise line to have your authentication working:

class User < ActiveRecord::Base

This line adds devise authenticable automatically for you inside your User class. You could also include the other modules as below:

# Same as using only devise, authenticable is activated by default
devise :authenticable

# Include authenticable + confirmable
devise :confirmable

# Include authenticable + recoverable
devise :recoverable

# Include authenticable + rememberable modules
devise :rememberable

# Include authenticable + confirmable + recoverable + rememberable + validatable
devise :confirmable, :recoverable, :rememberable, :validatable

# Same as above, include all of them
devise :all

# Include all except recoverable
devise :all, :except => :recoverable

Note that validations aren't added by default, so you're able to customize it. In order to have automatic validations working just include :validatable.

In addition to :except, you can provide some options to devise call:

  • pepper: setup a pepper to generate de encrypted password. By default no pepper is used:

    devise :all, :pepper => 'my_pepper'

  • stretches: configure how many times you want the password is reencrypted.

    devise :all, :stretches => 20

The next step after setting up your model is to configure your routes for devise. You do this by opening up your config/routes.rb and adding:

map.devise_for :users

This is going to look inside you User model and create the needed routes:

# Session routes for Authenticable (default)
     new_user_session GET  /users/sign_in                    {:controller=>"sessions", :action=>"new"}
         user_session POST /users/sign_in                    {:controller=>"sessions", :action=>"create"}
 destroy_user_session GET  /users/sign_out                   {:controller=>"sessions", :action=>"destroy"}

# Password routes for Recoverable, if User model has :recoverable configured
    new_user_password GET  /users/password/new(.:format)     {:controller=>"passwords", :action=>"new"}
   edit_user_password GET  /users/password/edit(.:format)    {:controller=>"passwords", :action=>"edit"}
        user_password PUT  /users/password(.:format)         {:controller=>"passwords", :action=>"update"}
                      POST /users/password(.:format)         {:controller=>"passwords", :action=>"create"}

# Confirmation routes for Confirmable, if User model has :confirmable configured
new_user_confirmation GET  /users/confirmation/new(.:format) {:controller=>"confirmations", :action=>"new"}
    user_confirmation GET  /users/confirmation(.:format)     {:controller=>"confirmations", :action=>"show"}
                      POST /users/confirmation(.:format)     {:controller=>"confirmations", :action=>"create"}

You can run the routes rake task to verify what routes are being created by devise. There are also some options available for configuring your routes:

  • :class_name => setup a different class to be looked up by devise, if it cannot be correctly find by the route name.

    map.devise_for :users, :class_name => 'Account'

  • :as => allows you to setup path name that will be used, as rails routes does. The following route configuration would setup your route as /accounts instead of /users:

    map.devise_for :users, :as => 'accounts'

  • :singular => setup the name used to create named routes. By default, for a :users key, it is going to be the singularized version, :user. To configure a named route like account_session_path instead of user_session_path just do:

    map.devise_for :users, :singular => :account

  • :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :password and :confirmation.

    map.devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }

And that is it! Devise is gonna create some helpers to use inside your controllers and views. To setup a controller that needs user authentication, just add this before_filter:

before_filter :authenticate_user!

To verify if a user is signed in, you have the following helper:


And to get the current signed in user this helper is available:


You have also access to the session for this scope:


After signing in a user, confirming it's account or updating it's password, devise will look for a scoped root path to redirect. Example: For a :user resource, it will use user_root_path if it exists, otherwise default root_path will be used.

Devise let's you setup as many roles as you want, so let's say you already have this User model and also want an Admin model with the same authentication stuff, but not confirmation or password recovery. Just follow the same steps:

# Create a migration with the required fields
create_table :admins do |t|

# Inside your Admin model
devise :validatable

# Inside your routes
map.devise_for :admin

# Inside your protected controller
before_filter :authenticate_admin!

# Inside your controllers and views


Devise check for flash messages using i18n, so you're able to customize them easily. For example, to change the sign in message you should setup your locale file this way:

      signed_in: 'Signed in successfully.'

You can also create distinct messages based on the resource you've configured:

        signed_in: 'Welcome user, you are signed in.'
        signed_in: 'Hello admin!'

Devise notifier uses the same pattern to create subject messages, but it is not able to know what scope you are, he just know the record (ie user instance) that was sent to it. So you need to customize messages based on the model class name (usually the same as the resource name, if you follow basic conventions):

      confirmation_instructions: 'Hello everybody!'
        confirmation_instructions: 'Hello User! Please confirm your email'
        reset_password_instructions: 'Reset instructions'

Take a look at our locale file to check all available messages.


Please refer to TODO file.

Bugs and Feedback

If you discover any bugs or want to drop a line, feel free to create an issue.

MIT License. Copyright 2009 Plataforma Tecnologia. blog.plataformatec.com.br