2009-09-16 08:17:43 -04:00
== Devise
2009-10-13 16:01:42 -04:00
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.
2009-10-19 23:28:01 -04:00
Right now it's composed of five mainly modules:
2009-10-13 16:01:42 -04:00
2009-10-30 06:29:10 -04:00
* Authenticatable: responsible for encrypting password and validating authenticity of a user while signing in.
2009-10-13 16:01:42 -04:00
* 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.
2009-10-19 22:52:31 -04:00
* Rememberable: manages generating and clearing token for remember the user from a saved cookie.
2009-10-13 16:43:40 -04:00
* Validatable: creates all needed validations for email and password. It's totally optional, so you're able to to customize validations by yourself.
2009-09-16 08:17:43 -04:00
2009-10-20 09:41:52 -04:00
There's an example application using Devise at http://github.com/plataformatec/devise_example .
2009-09-16 08:17:43 -04:00
== Dependencies
2009-10-20 15:40:50 -04:00
Devise is based on Warden (http://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).
2009-10-13 16:01:42 -04:00
== Installation
2009-10-20 22:21:06 -04:00
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/
2009-11-10 15:55:13 -05:00
Install warden gem if you don't have it installed (requires 0.5.2 or higher):
2009-10-13 16:01:42 -04:00
sudo gem install warden
2009-10-21 12:59:12 -04:00
Install devise gem:
2009-10-13 16:01:42 -04:00
2009-10-20 22:21:06 -04:00
sudo gem install devise
2009-10-13 16:01:42 -04:00
2009-10-21 12:59:12 -04:00
Configure warden and devise gems inside your app:
config.gem 'warden'
config.gem 'devise'
2009-10-18 13:25:16 -04:00
And you're ready to go.
2009-10-13 16:01:42 -04:00
== Basic Usage
2009-10-23 09:21:47 -04:00
This is a walkthrough with all steps you need to setup a devise resource, including model, migration, route files, and optional configuration. You can also check out the *Generators* section below to help you start.
2009-10-15 01:25:11 -04:00
2009-11-15 00:31:13 -05:00
Devise must be set up within the model (or models) you want to use, and devise routes must be created inside your routes.rb file.
2009-10-23 09:18:11 -04:00
2009-10-15 01:25:11 -04:00
We're assuming here you want a User model. First of all you have to setup a migration with the following fields:
2009-10-20 22:09:26 -04:00
create_table :users do
2009-10-30 06:29:10 -04:00
t.authenticatable
2009-10-20 22:09:26 -04:00
t.confirmable
t.recoverable
t.rememberable
t.timestamps
end
2009-10-19 22:52:31 -04:00
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
2009-10-15 01:25:11 -04:00
Now let's setup a User model adding the devise line to have your authentication working:
2009-10-13 16:01:42 -04:00
class User < ActiveRecord::Base
devise
end
2009-10-30 06:29:10 -04:00
This line adds devise authenticatable automatically for you inside your User class. Devise don't rely on _attr_accessible_ or _attr_protected_ inside its modules, so be sure to setup what attributes are accessible or protected in your model.
2009-10-22 15:30:00 -04:00
You could also include the other devise modules as below:
2009-10-13 16:01:42 -04:00
2009-10-30 06:29:10 -04:00
# Same as using only devise, authenticatable is activated by default
devise :authenticatable
2009-10-18 13:25:16 -04:00
2009-10-30 06:29:10 -04:00
# Include authenticatable + confirmable
2009-10-13 16:01:42 -04:00
devise :confirmable
2009-10-18 13:25:16 -04:00
2009-10-30 06:29:10 -04:00
# Include authenticatable + recoverable + rememberable
2009-10-30 05:23:47 -04:00
devise :recoverable, :rememberable
2009-10-18 13:25:16 -04:00
2009-10-30 05:23:47 -04:00
# Include all of them
2009-10-13 16:01:42 -04:00
devise :all
2009-10-18 13:25:16 -04:00
# Include all except recoverable
devise :all, :except => :recoverable
2009-10-13 16:01:42 -04:00
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.
2009-11-03 07:11:36 -05:00
== Model configuration
2009-10-30 05:23:47 -04:00
2009-11-15 00:31:13 -05:00
In addition to :except, you can provide :pepper, :stretches, :encryptor, :authentication_keys, :confirm_within and :remember_for as options to devise method.
2009-10-20 09:08:40 -04:00
2009-11-03 07:11:36 -05:00
All those options are described in "config/initializers/devise.rb", which is generated when you invoke `ruby script/generate devise_install` in your application root.
2009-10-30 05:23:47 -04:00
== Routes
2009-10-13 16:01:42 -04:00
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
2009-11-19 08:16:31 -05:00
This is going to look inside you User model and create a set of needed routes (you can see them by running `rake routes`).
2009-10-13 16:01:42 -04:00
2009-11-19 08:16:31 -05:00
There are also some options available for configuring your routes, as :class_name (to set the class for that route), :as and :path_names, where the last two have the same meaning as in common routes. The available :path_names are:
2009-10-13 16:01:42 -04:00
2009-11-03 07:11:36 -05:00
map.devise_for :users, :as => "usuarios", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
2009-10-17 11:10:15 -04:00
2009-11-03 07:11:36 -05:00
Be sure to check devise_for documentation for detailed description.
2009-10-13 16:01:42 -04:00
2009-11-19 08:16:31 -05:00
== Controller filters and helpers
2009-10-30 05:23:47 -04:00
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:
2009-10-13 16:01:42 -04:00
2009-10-18 13:25:16 -04:00
before_filter :authenticate_user!
2009-10-13 16:01:42 -04:00
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
2009-10-18 16:31:01 -04:00
You have also access to the session for this scope:
user_session
2009-10-21 12:59:12 -04:00
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. To do it so, you need to create e default root inside your routes for your application:
map.root :controller => 'home'
2009-11-19 08:16:31 -05:00
You can also overwrite after_sign_in_path_for and after_sign_out_path_for to customize better your redirect hooks.
Finally, if you are using confirmable or recoverable, you also need to setup default url options for the mailer. Here's is the configuration for development:
2009-10-21 12:59:12 -04:00
2009-11-02 20:14:27 -05:00
DeviseMailer.sender = "no-reply@yourapp.com"
2009-11-06 06:33:18 -05:00
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
2009-10-18 22:38:50 -04:00
2009-10-30 05:23:47 -04:00
== Tidying up
2009-10-13 16:01:42 -04:00
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:
2009-10-15 01:25:11 -04:00
# Create a migration with the required fields
2009-10-20 22:09:26 -04:00
create_table :admins do |t|
2009-10-30 06:29:10 -04:00
t.authenticatable
2009-10-20 22:09:26 -04:00
end
2009-10-18 13:25:16 -04:00
2009-10-13 16:01:42 -04:00
# Inside your Admin model
devise :validatable
2009-10-18 13:25:16 -04:00
2009-10-13 16:01:42 -04:00
# Inside your routes
map.devise_for :admin
2009-10-18 13:25:16 -04:00
2009-10-13 16:01:42 -04:00
# Inside your protected controller
2009-10-18 16:31:01 -04:00
before_filter :authenticate_admin!
2009-10-18 13:25:16 -04:00
2009-10-13 16:01:42 -04:00
# Inside your controllers and views
admin_signed_in?
current_admin
2009-10-18 16:31:01 -04:00
admin_session
2009-10-13 16:01:42 -04:00
2009-10-23 09:18:11 -04:00
== Generators
Devise comes with some generators to help you start:
2009-11-03 07:11:36 -05:00
ruby script/generate devise_install
This will generate an initializer, with a description of all configuration values. You can also generate models through:
ruby script/generate devise Model
2009-10-23 09:18:11 -04:00
2009-11-03 07:11:36 -05:00
A model configured with all devise modules and attr_accessible for default fields will be created. The generator will also create the migration and configure your routes for devise.
2009-10-23 09:18:11 -04:00
You can also copy devise views to your application, being able to modify them based on your needs. To do it so, run the following command:
2009-11-03 07:11:36 -05:00
ruby script/generate devise_views
2009-10-23 09:18:11 -04:00
2009-11-02 20:14:27 -05:00
This is gonna copy all session, password, confirmation and mailer views to your app/views folder.
2009-10-23 09:18:11 -04:00
2009-10-19 23:28:01 -04:00
== I18n
2009-10-29 05:14:38 -04:00
Devise uses flash messages with I18n with the flash keys :success and :failure. To customize your app, you can setup your locale file this way:
2009-10-19 23:28:01 -04:00
en:
devise:
sessions:
signed_in: 'Signed in successfully.'
2009-10-29 05:14:38 -04:00
You can also create distinct messages based on the resource you've configured using the singular name given in routes:
2009-10-19 23:28:01 -04:00
en:
devise:
sessions:
user:
signed_in: 'Welcome user, you are signed in.'
admin:
signed_in: 'Hello admin!'
2009-11-02 20:14:27 -05:00
Devise mailer uses the same pattern to create subject messages:
2009-10-19 23:28:01 -04:00
en:
devise:
2009-11-02 20:14:27 -05:00
mailer:
2009-10-19 23:28:01 -04:00
confirmation_instructions: 'Hello everybody!'
user:
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.
2009-11-16 11:58:14 -05:00
== Test helpers
Devise includes some tests helpers for functional specs. To use them, you just
need to include Devise::TestHelpers in your test class and use the sign_in and
sign_out method. Such methods have the same signature as in controllers:
sign_in :user, @user # sign_in(scope, resource)
sign_in @user # sign_in(resource)
sign_out :user # sign_out(scope)
sign_out @user # sign_out(resource)
2009-11-20 18:47:20 -05:00
You can include the Devise Test Helpers in all of your tests by adding the
following to the bottom of your test/test_helper.rb or spec/spec_helper.rb file:
class ActionController::TestCase
include Devise::TestHelpers
end
2009-11-10 10:29:52 -05:00
== Migrating from other solutions
Devise implements encryption strategies for Clearance, Authlogic and Restful-Authentication. To make use of it set the desired encryptor in the encryptor initializer config option. You might also need to rename your encrypted password and salt columns to match Devises's one (encrypted_password and password_salt).
2009-11-13 19:33:00 -05:00
== Other ORMs
Devise was made to work from scratch with ActiveRecord. However it currently supports MongoMapper as well.
To use it, just set Devise.orm or configure it in the initialization file (which is created with devise_install).
2009-10-13 16:01:42 -04:00
== TODO
Please refer to TODO file.
2009-11-10 15:55:13 -05:00
== Contributors
* José Valim (http://github.com/josevalim)
* Carlos Antônio da Silva (http://github.com/carlosantoniodasilva)
* Marcelo Silveira (http://github.com/mhfs)
2009-11-13 19:33:00 -05:00
* Cyril Mougel (http://github.com/shingara)
2009-11-10 15:55:13 -05:00
2009-10-13 16:01:42 -04:00
== Bugs and Feedback
2009-09-16 08:17:43 -04:00
2009-10-22 23:32:22 -04:00
If you discover any bugs or want to drop a line, feel free to create an issue on
GitHub or send an e-mail to the mailing list.
http://github.com/plataformatec/devise/issues
http://groups.google.com/group/plataformatec-devise
2009-09-16 08:17:43 -04:00
2009-10-13 16:01:42 -04:00
MIT License. Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br