Devise is based on Warden (http://github.com/hassox/warden), a Rack Authentication Framework from hassox (http://github.com/hassox), so you're gonna need to install this gem. Current warden version is 0.4.0. Please ensure you have it installed in order to use devise (see instalation below).
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 confirmable
devise :confirmable
# Include recoverable
devise :recoverable
# Include validatable
devise :validatable
# Include all of them
devise :confirmable, :recoverable, :validatable
# Same as above, include all of them
devise :all
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.
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:
* :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:
* :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:
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 :sign_in_user!
To verify if a user is signed in, you have the following helper:
user_signed_in?
And to get the current signed in user this helper is available:
current_user
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: