1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00
heartcombo--devise/README.rdoc

354 lines
16 KiB
Text
Raw Normal View History

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.
It's composed of 12 modules:
2009-10-13 16:01:42 -04:00
* Database Authenticatable: encrypts and stores a password in the database to validate the authenticity of a user while signing in. The authentication can be done both through POST requests or HTTP Basic Authentication.
* Token Authenticatable: signs in a user based on an authentication token (also known as "single access token"). The token can be given both through query string or HTTP Basic Authentication.
2010-10-15 02:33:23 -04:00
* Omniauthable: adds Omniauth (github.com/intridea/omniauth) support;
* Confirmable: sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.
* Recoverable: resets the user password and sends reset instructions.
2010-05-11 05:37:46 -04:00
* Registerable: handles signing up users through a registration process, also allowing them to edit and destroy their account.
* Rememberable: manages generating and clearing a token for remembering the user from a saved cookie.
* Trackable: tracks sign in count, timestamps and IP address.
* Timeoutable: expires sessions that have no activity in a specified period of time.
* Validatable: provides validations of email and password. It's optional and can be customized, so you're able to define your own validations.
* Lockable: locks an account after a specified number of failed sign-in attempts. Can unlock via email or after a specified time period.
2010-11-30 08:17:18 -05:00
* Encryptable: adds support of other authentication mechanisms besides the built-in Bcrypt (the default).
2009-09-16 08:17:43 -04:00
== Information
2009-10-13 16:01:42 -04:00
=== The Devise wiki
2009-10-20 22:21:06 -04:00
The Devise Wiki has lots of additional information about Devise including many "how-to" articles and answers to the most frequently asked questions. Please browse the Wiki after finishing this README:
2010-06-28 15:25:03 -04:00
http://wiki.github.com/plataformatec/devise
=== Bug reports
2009-12-12 19:52:48 -05:00
If you discover a problem with Devise, we would like to know about it. However, we ask that you please review these guidelines before submitting a bug report:
2010-05-21 10:42:34 -04:00
http://github.com/plataformatec/devise/wiki/Bug-reports
2010-05-21 10:42:34 -04:00
If you found a security bug, do *NOT* use the GitHub issue tracker. Send email or a private GitHub message to the maintainers listed at the bottom of the README.
=== Mailing list
2010-04-05 06:21:02 -04:00
If you have any questions, comments, or concerns, please use the Google Group instead of the GitHub issue tracker:
http://groups.google.com/group/plataformatec-devise
=== RDocs
You can view the Devise documentation in RDoc format here:
http://rubydoc.info/github/plataformatec/devise/master/frames
If you need to use Devise with Rails 2.3, you can always run `gem server` from the command line after you install the gem to access the old documentation.
=== Example applications
There are a few example applications available on GitHub that demonstrate various features of Devise with different versions of Rails. You can view them here:
http://github.com/plataformatec/devise/wiki/Example-Applications
=== Extensions
Our community has created a number of extensions that add functionality above and beyond what is included with Devise. You can view a list of available extensions and add your own here:
http://github.com/plataformatec/devise/wiki/Extensions
=== Contributing
We hope that you will consider contributing to Devise. Please read this short overview for some information about how to get started:
http://github.com/plataformatec/devise/wiki/Contributing
You will usually want to write tests for your changes. To run the test suite, `cd` into Devise's top-level directory and run `bundle install` and `rake`. For the tests to pass, you will need to have a MongoDB server (version 1.6 or newer) running on your system.
== Installation
You can use the latest Rails 3 gem with the latest Devise gem:
gem install devise
After you install Devise and add it to your Gemfile, you need to run the generator:
rails generate devise:install
The generator will install an initializer which describes ALL Devise's configuration options and you MUST take a look at it. When you are done, you are ready to add Devise to any of your models using the generator:
rails generate devise MODEL
2011-04-18 04:03:36 -04:00
Replace MODEL by the class name used for the applications users, it's frequently 'User' but could also be 'Admin'. This will create a model (if one does not exist) and configure it with default Devise modules. Next, you'll usually run db:migrate as the generator will have created a migration file (if your ORM supports them). This generator also configures your config/routes.rb file, continue reading this file to understand exactly what the generator produces and how to use it.
Support for Rails 2.3.x can be found by installing Devise 1.0.x from the v1.0 branch.
2011-01-31 08:06:35 -05:00
== Starting with Rails?
2011-01-31 09:16:58 -05:00
If you are building your first Rails application, we recommend you to *not* use Devise. Devise requires a good understanding of the Rails Framework. In such cases, we advise you to start a simple authentication system from scratch, today we have two resources:
2011-01-31 08:06:35 -05:00
* Michael Hartl's online book: http://railstutorial.org/chapters/modeling-and-viewing-users-two#top
* Ryan Bates' Railscast: http://railscasts.com/episodes/250-authentication-from-scratch
Once you have solidified you understanding of Rails and authentication mechanisms, we assure you Devise will be very pleasant to work with. :)
== Getting started
2009-10-13 16:01:42 -04:00
2010-05-21 10:42:34 -04:00
This is a walkthrough with all steps you need to setup a devise resource, including model, migration, route files, and optional configuration.
Devise must be set up within the model (or models) you want to use. Devise routes must be created inside your config/routes.rb file.
2009-10-23 09:18:11 -04:00
We're assuming here you want a User model with some Devise modules, as outlined below:
2010-01-13 13:45:24 -05:00
class User < ActiveRecord::Base
2011-06-28 06:19:00 -04:00
devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable
2010-01-13 13:45:24 -05:00
end
After you choose which modules to use, you need to set up your migrations. Luckily, Devise has some helpers to save you from this boring work:
2009-12-28 12:48:38 -05:00
create_table :users do |t|
t.database_authenticatable
2009-10-20 22:09:26 -04:00
t.confirmable
t.recoverable
t.rememberable
t.trackable
2009-10-20 22:09:26 -04:00
t.timestamps
end
2009-10-19 22:52:31 -04:00
Devise doesn't use _attr_accessible_ or _attr_protected_ inside its modules, so be sure to define attributes as accessible or protected in your model.
2009-10-13 16:01:42 -04:00
Configure your routes after setting up your model. Open your config/routes.rb file and add:
2009-10-13 16:01:42 -04:00
devise_for :users
2009-10-13 16:01:42 -04:00
2010-07-18 18:31:51 -04:00
This will use your User model to create a set of needed routes (you can see them by running `rake routes`). If you invoked the devise generator, you noticed that this is exactly what the generator produces for us: model, routes and migrations.
2009-10-13 16:01:42 -04:00
2010-07-18 18:31:51 -04:00
Don't forget to run rake db:migrate and you are ready to go! But don't stop reading here, we still have a lot to tell you.
2010-01-13 13:45:24 -05:00
=== Controller filters and helpers
2009-10-30 05:23:47 -04:00
Devise will create some helpers to use inside your controllers and views. To set up a controller with user authentication, just add this before_filter:
2009-10-13 16:01:42 -04:00
before_filter :authenticate_user!
2009-10-13 16:01:42 -04:00
To verify if a user is signed in, use the following helper:
2009-10-13 16:01:42 -04:00
user_signed_in?
For the current signed-in user, this helper is available:
2009-10-13 16:01:42 -04:00
current_user
You can access the session for this scope:
2009-10-18 16:31:01 -04:00
user_session
After signing in a user, confirming the account or updating the 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. This means that you need to set the root inside your routes:
root :to => "home#index"
You can also overwrite after_sign_in_path_for and after_sign_out_path_for to customize your redirect hooks.
Finally, you need to set up default url options for the mailer in each environment. Here is the configuration for config/environments/development.rb:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Notice that if your devise model is not called "user" but "member", then the helpers you should use are:
before_filter :authenticate_member!
member_signed_in?
current_member
member_session
=== Configuring Models
The devise method in your models also accepts some options to configure its modules. For example, you can choose which encryptor to use in database_authenticatable:
2011-06-28 06:19:00 -04:00
devise :database_authenticatable, :registerable, :confirmable, :recoverable, :stretches => 20
Besides :stretches, you can define :pepper, :encryptor, :confirm_within, :remember_for, :timeout_in, :unlock_in and other values. For details, see the initializer file that was created when you invoked the "devise:install" generator described above.
=== Configuring multiple models
2009-10-30 05:23:47 -04:00
Devise allows you to set up as many roles as you want. For example, you may have a User model and also want an Admin model with just authentication, trackable, lockable and timeoutable features and no confirmation or password-recovery features. Just follow these steps:
2009-10-13 16:01:42 -04:00
# Create a migration with the required fields
2009-10-20 22:09:26 -04:00
create_table :admins do |t|
t.database_authenticatable
2010-01-13 12:46:04 -05:00
t.lockable
t.trackable
t.timestamps
2009-10-20 22:09:26 -04:00
end
2009-10-13 16:01:42 -04:00
# Inside your Admin model
devise :database_authenticatable, :trackable, :timeoutable, :lockable
2009-10-13 16:01:42 -04:00
# Inside your routes
devise_for :admins
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-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
=== Configuring views
2010-01-13 13:45:24 -05:00
2010-04-12 07:48:22 -04:00
We built Devise to help you quickly develop an application that uses authentication. However, we don't want to be in your way when you need to customize it.
2009-10-23 09:18:11 -04:00
2010-04-12 07:48:22 -04:00
Since Devise is an engine, all its views are packaged inside the gem. These views will help you get started, but after sometime you may want to change them. If this is the case, you just need to invoke the following generator, and it will copy all views to your application:
2009-10-23 09:18:11 -04:00
rails generate devise:views
2010-01-13 13:45:24 -05:00
2010-07-15 07:01:31 -04:00
If you have more than one role in your application (such as "User" and "Admin"), you will notice that Devise uses the same views for all roles. Fortunately, Devise offers an easy way to customize views. All you need to do is set "config.scoped_views = true" inside "config/initializers/devise.rb".
After doing so, you will be able to have views based on the role like "users/sessions/new" and "admins/sessions/new". If no view is found within the scope, Devise will use the default view at "devise/sessions/new". You can also use the generator to generate scoped views:
rails generate devise:views users
=== Configuring controllers
2010-04-12 07:48:22 -04:00
If the customization at the views level is not enough, you can customize each controller by following these steps:
1) Create your custom controller, for example a Admins::SessionsController:
class Admins::SessionsController < Devise::SessionsController
end
2010-04-12 07:48:22 -04:00
2) Tell the router to use this controller:
2010-07-15 07:01:31 -04:00
devise_for :admins, :controllers => { :sessions => "admins/sessions" }
2010-04-12 07:48:22 -04:00
3) And since we changed the controller, it won't use the "devise/sessions" views, so remember to copy "devise/sessions" to "admin/sessions".
Remember that Devise uses flash messages to let users know if sign in was successful or failed. Devise expects your application to call "flash[:notice]" and "flash[:alert]" as appropriate.
=== Configuring routes
2010-07-18 18:31:51 -04:00
Devise also ships with default routes. If you need to customize them, you should probably be able to do it through the devise_for method. It accepts several options like :class_name, :path_prefix and so on, including the possibility to change path names for I18n:
devise_for :users, :path => "usuarios", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
Be sure to check devise_for documentation for details.
If you have the need for more deep customization, for instance to also allow "/sign_in" besides "/users/sign_in", all you need to do is to create your routes normally and wrap them in a +devise_scope+ block in the router:
2010-08-02 07:22:45 -04:00
devise_scope :user do
get "sign_in", :to => "devise/sessions#new"
end
This way you tell devise to use the scope :user when "/sign_in" is accessed. Notice +devise_scope+ is also aliased as +as+ and you can also give a block to +devise_for+, resulting in the same behavior:
devise_for :users do
get "sign_in", :to => "devise/sessions#new"
end
2010-07-18 18:31:51 -04:00
2010-08-02 07:22:45 -04:00
Feel free to choose the one you prefer!
2010-07-18 18:31:51 -04:00
=== I18n
2009-10-19 23:28:01 -04:00
Devise uses flash messages with I18n with the flash keys :notice and :alert. To customize your app, you can set up your locale file:
2009-10-19 23:28:01 -04:00
en:
devise:
sessions:
signed_in: 'Signed in successfully.'
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!'
2010-07-15 07:01:31 -04:00
The Devise mailer uses a similar pattern to create subject messages:
2009-10-19 23:28:01 -04:00
en:
devise:
mailer:
2010-07-15 07:01:31 -04:00
confirmation_instructions:
subject: 'Hello everybody!'
user_subject: 'Hello User! Please confirm your email'
reset_password_instructions:
subject: 'Reset instructions'
2009-10-19 23:28:01 -04:00
Take a look at our locale file to check all available messages. You may also be interested in one of the many translations that are available on our wiki:
2009-10-19 23:28:01 -04:00
http://github.com/plataformatec/devise/wiki/I18n
=== Test helpers
2009-11-16 11:58:14 -05:00
2009-12-07 15:37:04 -05:00
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:
2009-11-16 11:58:14 -05:00
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)
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 file:
class ActionController::TestCase
include Devise::TestHelpers
end
If you're using RSpec and want the helpers automatically included within all +describe+ blocks, add a file called spec/support/devise.rb with the following contents:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
Do not use such helpers for integration tests such as Cucumber or Webrat. Instead, fill in the form or explicitly set the user in session. For more tips, check the wiki (http://wiki.github.com/plataformatec/devise).
2009-12-07 15:37:04 -05:00
2011-04-11 04:26:36 -04:00
=== Omniauth
2011-04-11 04:26:36 -04:00
Devise comes with Omniauth support out of the box to authenticate from other providers. You can read more about Omniauth support in the wiki:
2010-04-22 14:55:27 -04:00
2011-04-11 04:26:36 -04:00
* https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
2010-08-17 18:38:07 -04:00
=== Other ORMs
Devise supports ActiveRecord (default) and Mongoid. To choose other ORM, you just need to require it in the initializer file.
=== Migrating from other solutions
Devise implements encryption strategies for Clearance, Authlogic and Restful-Authentication. To make use of these strategies, you need set the desired encryptor in the encryptor initializer config option and add :encryptable to your model. You might also need to rename your encrypted password and salt columns to match Devise's fields (encrypted_password and password_salt).
== Additional information
=== Warden
2009-11-10 15:55:13 -05:00
Devise is based on Warden, which is a general Rack authentication framework created by Daniel Neighman. We encourage you to read more about Warden here:
2010-07-15 07:01:31 -04:00
http://github.com/hassox/warden
=== Contributors
2010-05-03 08:00:32 -04:00
We have a long list of valued contributors. Check them all at:
http://github.com/plataformatec/devise/contributors
2009-11-10 15:55:13 -05:00
=== Maintainers
2010-07-15 07:01:31 -04:00
* José Valim (http://github.com/josevalim)
* Carlos Antônio da Silva (http://github.com/carlosantoniodasilva)
== License
MIT License. Copyright 2010 Plataforma Tecnologia. http://blog.plataformatec.com.br